C# Script Examples

// PC_Projectile Script 2017//

using UnityEngine;
using System.Collections;
public class Fire : MonoBehaviour // Named the class Fire//
{
 public Rigidbody Box_v1;  //Adding 3 public variables that will fire as projectiles with mouse clicks//
public Rigidbody Box_v2;
public Rigidbody Canister_v1;
 public float speed = 50f; // Adding a speed adjustment using float variable//
public Transform Barrel; // Use this game object as the projectile caster//
public float fireRate; // Adding fire rate to delay the amount of time we can fire//
private float nextFire;//Adjusting when one can fire projectiles in a certain amount of time//
void Box1 ()
{
Ray aimRay = Camera.main.ScreenPointToRay(Input.mousePosition);   // Cast a Ray from the Camera //
RaycastHit aimRayHit;//If the Ray hits any surface it clones the prefab and adds velocity//
Rigidbody Clone = (Rigidbody) Instantiate(Box_v1, Barrel.position,Barrel.rotation);  //A Rigidbody is added, although if the prefab has one already this gets ignored.
Clone.velocity = transform.forward * speed;  //This is what causes forward momentum
}
    void Box2()
{
Ray aimRay = Camera.main.ScreenPointToRay(Input.mousePosition);// Cast a Ray from the Camera //
RaycastHit aimRayHit;;//If the Ray hits any surface it clones the prefab and adds velocity//
Rigidbody Clone = (Rigidbody)Instantiate(Box_v2, Barrel.position, Barrel.rotation);
Clone.velocity = transform.forward * speed; //This is what causes forward momentum
}
    void Can()
{
Ray aimRay = Camera.main.ScreenPointToRay(Input.mousePosition);// Cast a Ray from the Camera //
RaycastHit aimRayHit;;//If the Ray hits any surface it clones the prefab and adds velocity//
Rigidbody Clone = (Rigidbody)Instantiate(Canister_v1, Barrel.position, Barrel.rotation);
Clone.velocity = transform.forward * speed; //This is what causes forward momentum
}
void Update ()
{
if (Input.GetMouseButtonDown(0) && Time.time > nextFire)   // Update checks to see if the player is pushing the Left mouse button on each frame plus it’s also keeping track of the last time the player fired by changing the nextFire integer variable. When both the  conditions are active meaning player pushed the Left Mouse button and the last time the user fired was over 1 second ago. //
{
nextFire = Time.time + fireRate;   //Unity adds a new value nextFire //
Box1(); //Tells Unity to run the method called Box1
}
        if (Input.GetMouseButtonDown(1) && Time.time > nextFire) // Update checks to see if the player is pushing the Right mouse button…//
{
nextFire = Time.time + fireRate;
Box2();//Tells Unity to run the method called Box2
}
        if (Input.GetMouseButtonDown(2) && Time.time > nextFire)   // Update checks to see if the player is pushing the Middle mouse button…//
{
{
nextFire = Time.time + fireRate;
Can();//Tells Unity to run the method called Can
}
}
}

I thought I would share some insight for those studying Unity 5.6 and C# scripting basics. I wanted to show a couple common ways one can add movement to a game object. I’ll show you how to add force to the Rigidbody component and also how to directly effect the transform component using C# scripting basics.

PC 2017