Showing posts with label lynda. Show all posts
Showing posts with label lynda. Show all posts

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);
}
}
}







Tuesday, April 23, 2013

C# versions of the Lynda.com Unity course scripts

I recently completed the Unity 3.5 essentials course on Lynda.com in order to get past the limitations of the drag and drop features.  The course uses Javascript, but I prefer C#.  I'm putting my converted code here for anyone else that would like it. 

I use the internal instead of private access modifier as this will hide the variable from the inspector, but still make it available to other classes.


Final RotationController class.

using UnityEngine;
using System.Collections;

public class RotationController : MonoBehaviour {
public bool RotationState = true;
public float RotationSpeed = 20f;
internal float initialSpeed;
// Use this for initialization
void Start () {
initialSpeed = RotationSpeed;
}
// Update is called once per frame
void Update ()
{
if(RotationState && RotationSpeed > 5f)
{
transform.Rotate(0, RotationSpeed * Time.deltaTime, 0);
}
}
void OnMouseDown()
{
RotationState = !RotationState;
RotationSpeed -= 0.5f;
if (RotationSpeed < 0)
RotationSpeed = initialSpeed;
}
}

Final ReportDistance Class

using UnityEngine;
using System.Collections;

public class ReportDistance : MonoBehaviour {
public Transform targetObject;

// Use this for initialization
void Start () 
{
targetObject = GameObject.Find("First Person Controller").transform;
}
// Update is called once per frame
void Update () {
if (targetObject)
{
float distance = Vector3.Distance(targetObject.position, this.transform.position);
//Debug.Log("Distance to target object: " + distance);
}
}
}

Final RayCaster Class

using UnityEngine;
using System.Collections;

public class RayCaster : MonoBehaviour {
RaycastHit hit; //holds properties of detected object

// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () 
{
if(Physics.Raycast(transform.position, transform.forward, out hit, 10))
Debug.Log("There is a " + hit.transform.name + " something in front of the object!");
else Debug.Log("There is nothing within 10 metres!");
}
}