No errors, but what I've got doesn't work and I cannot figure out why. All I'm looking for is the variable called health to go down by 1 point with every movement in the inspector. The script is attached to a player prefab. N.B. Even if I change the value of the variable in the script it remains the same in the inspector. Here's the code. Some help would be much appreciated.
#pragma strict
var hit : RaycastHit;
var utran;
var newTile : GameObject;
public var health : int;
function Start () {
health = 300;
}
function Update () {
utran = new Vector3 (transform.position.x, transform.position.y, transform.position.z-0.5);
if (Input.GetKeyUp(KeyCode.A))
{
if (Physics.Raycast (transform.position, -Vector3.right, hit, 25))
{
if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
{
transform.position.x -= 2.75;
health -= 1;
}
}
}
if (Input.GetKeyUp(KeyCode.D))
{
if (Physics.Raycast (transform.position, Vector3.right, hit, 25))
{
if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
{
transform.position.x += 2.75;
health -= 1;
}
}
}
if (Input.GetKeyUp(KeyCode.W))
{
if (Physics.Raycast (transform.position, Vector3.up, hit, 25))
{
if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
{
transform.position.y += 2.75;
health -= 1;
}
}
}
if (Input.GetKeyUp(KeyCode.S))
{
if (Physics.Raycast (transform.position, -Vector3.up, hit, 25))
{
if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
{
transform.position.y -= 2.75;
health -= 1;
}
}
}
if (Physics.Raycast (utran, Vector3.forward, hit, 5))
{
if (hit.transform.tag == "Exit")
{
Application.LoadLevel(Application.loadedLevel);
}
}
if (Physics.Raycast (utran, Vector3.forward, hit, 5))
{
if (hit.transform.tag == "Tassel")
{
Destroy (hit.transform.gameObject);
Instantiate (newTile, hit.transform.position, hit.transform.rotation);
}
}
}
↧