Character Shoot Code

This code is how the characters will shoot and apply damage. Lets get straight into it!

So, the first thing here is that the code is calling the CharacterStat script and the game objects. Since we need the character stats to indicate whether the opponent is within range.

Then, in void start, it will immediately call for the stats for the range of the characters.

In void Update, every frame it will check whether the player has pressed the left mouse button. If the player has pressed the left mouse button, it will start the GetShootTarget ting.

So, the same as the character movement script, a ray will be cast from the camera. It will track the mouse position on the screen. If the mouse is clicked on an object that has the tag “Enemy”, then the target will be that game object. It will also say Target Spotted!

This is where the ApplyDamage is. Pretty much, when the target is picked, it will check whether the distance between them and get that distance. It will then check whether they are in range of the characters.

THEN, it will use a random number between 0 to 100, using the accuracy stat, to see whether they hit or not. If the number the computer gets is less than or equal to the accuracy stat, then they will hit. When hit, it will alter the hit points of the enemy depending on the weapon damage. If they miss, then that is that. The console will say I missed!

Quite fun stuff

So, I made one small change here. I added the current amount of attacks statistic, so that the function is called multiple times until all shots of the weapon is fired. Everything else is the same.

Leave a comment

Design a site like this with WordPress.com
Get started