Week 5 – Fire Code

This is the code for the second part of the shooting mechanic. As mentioned before it creates the projectile round at a certain time and desired position.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProjectileFire : MonoBehaviour {

public float _timeRounds = 0.15f;
// Sets the time between each projectile, when they are created; Simply put how often a gun will shoot

public GameObject projectile;
// What the gun will actually shoot

float _nextRound;
// Determines when the next projectile can be fired

// Use this for initialization
void Awake () {

_nextRound = 0f;
// Default value for next projectile is instantly

}

// Update is called once per frame
void Update () {

CharacterControl _player = transform.root.GetComponent<CharacterControl> ();
// Ties the current script to the Character Control as to get the direction the player is facing, before creating the bullet; The root section is used, so that the script wanted will be searched to the upmost level, not through other children of the player character

// Next there will be a control to see if the player is pushing the fire button and the time for the next projectile to be created has passed
if(Input.GetAxisRaw(“Fire1”)> 0 && _nextRound< Time.time){

_nextRound = Time.time + _timeRounds;
// Determine when the next projectile can be fired

Vector3 _rotation;
// Used to rotate the projectile object, as it must shoot from the direction the player is facing

// A verification will be made as to determine the direction the player is facing, by using the Direction function from CharacterControl script
if (_player.Direction () == -1f){
_rotation = new Vector3 (0, -90, 0);
//If the player is facing left, this line will switch the direction of the projectile to face the left direction
}
else {
_rotation = new Vector3 (0, 90, 0);
//If the player is facing right, after it has faced left, the line will switch again the projectile, so it faces the right direction
}

Instantiate (projectile, transform.position, Quaternion.Euler (_rotation));
// The projectile will be created at the position of the muzzle, and it will also be shot from the direction the player is facing; Quaternion is a mathematical term, which allows rotation on all axes, in this case it is a Unity method, which allows the usage of the _rotation vector, which is modified, depending on the way the player is facing
}

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s