Thursday, April 25, 2013

More C# conversions of the Unity scripts for the Lynda.com Unity essentials course


Projectile Class

public class Projectile : MonoBehaviour {

public GameObject projectile;
public float FireRate = 0.5f;
public float Speed = 5f;
internal float nextFire;

void Update ()
{
if (Input.GetButton ("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + FireRate;
GameObject clone = (GameObject)Instantiate(projectile, transform.position, transform.rotation);
clone.rigidbody.velocity = transform.TransformDirection(new Vector3(0,0,Speed));
Physics.IgnoreCollision(clone.collider, transform.root.collider); //stops projectile from colliding with player
}
}
}

Final Projectile Handler Class


public class ProjectileHandler : MonoBehaviour {

void Start ()
{
//Destroy(gameObject, 5);  // destroy object after set time
}

void Update () {

}

void OnCollisionEnter(Collision collison)
{
if (collison.transform.tag != "Dont Destroy")
{
//send message to collided object
collison.transform.SendMessage("BeenHit", SendMessageOptions.DontRequireReceiver);

//Debug.Log(collison.transform.name);

Destroy(gameObject, 1);
}
else
{
Destroy(rigidbody, 5);
//Debug.Log("stopped " + collison.transform.name);
}
}
}

DoorDamage Controller


public class DoorDamageController : MonoBehaviour {

public Collider doorJam;
public Collider wall;

void BeenHit()
{
//Destroy(gameObject, 1);
Physics.IgnoreCollision(doorJam.collider, transform.collider);
Physics.IgnoreCollision(wall.collider, transform.collider);
}
}

GameManager


public class GameManager : MonoBehaviour {

public GameObject fpc;

// Use this for initialization
void Start ()
{
//hide cursor
Screen.showCursor = false;

}

// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown("escape"))
{
Screen.showCursor = !Screen.showCursor;
fpc.SendMessage("ToggleInput", Screen.showCursor);
}
}
}