Week 5 – Projectile Code

This is the code that I did for the projectile, in regards to the shooting mechanic.

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

public class ShootProjectile : MonoBehaviour {

public float _range = 10f;
// How far the bullet is going to go

public float _damage = 5f;
// The damage the bullet is going to do – can be changed on the fly, with possible change for each weapon

Ray _trajectory;
// It is the actual trajectory of the bullet, and detects what the bullet will hit

RaycastHit _hit;
// The information that gets returned, when the ray hits something

int _shotMask;
// Using layerMask is to make sure that the bullet can shoot through some obects and through some not

LineRenderer _bullet;
// Make use of the line renderer to see the trajectory of the ray

// Use this for initialization
void Awake () {
_shotMask = LayerMask.GetMask (“Shooted”);
// Assign the variable the layerMask for the objects that can be shot at

_bullet = GetComponent<LineRenderer> ();
// Connects the bullet to the line renderer

_trajectory.origin = transform.position;
// The ray will be shot from were it has been created, for example the position from were the actual projectile has been created

_trajectory.direction = transform.forward;
// The ray will be shot forward from the actual transform

_bullet.SetPosition (0, transform.position);
// Sets the position of were the line will start drawing itself and finish itself

// Under, a ray will be shot, and the information, after it hits, will be stored in the _hit value
if(Physics.Raycast(_trajectory, out _hit, _range, _shotMask)){

// Enemies will be coded here for damage

_bullet.SetPosition(1, _hit.point);
// Sets the end of the line rendering
else _bullet.SetPosition(1, _trajectory.origin + _trajectory.direction * _range);
// If it does not hit anything, the ray will stop once it reaches its maximum range, which is defined in the unity editor


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



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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s