So, I’ve looked everywhere for the answer to this, but basically I am following a tutorial for a survival game, (Brackeys) and for some reason, instead of the Raycast that gets the distance of the object I am looking at, it just get’s the distance of the terrain. It doesn’t work on Prefabs or trees, just the terrain.
The language I’m using is JavaScript. Please help, work on my project has halted since this.
My code for the weapon is:
#pragma strict
var theDamage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
var TheSystem : Transform;
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
//Attack animation - IF YOU ARE GETTING ERRORS BECAUSE YOU HAVENT MADE AN ANIMATION AND ATTACHED IT, DELETE THE FOLOWING LINE.
GetComponent.<Animation>().Play("AttackMace");
}
// if (theMace.GetComponent.<Animation>().isPlaying == false)
// {
// theMace.GetComponent.<Animation>().CrossFade("Idle");
// }
//
// if (Input.GetKey (KeyCode.LeftShift))
// {
// theMace.GetComponent.<Animation>().CrossFade("Sprint");
// }
//
// if (Input.GetKeyUp(KeyCode.LeftShift))
// {
// theMace.GetComponent.<Animation>().CrossFade("Idle");
// }
}
function AttackDamage ()
{
//Attack function
var hit : RaycastHit;
if (Physics.Raycast (TheSystem.transform.position, TheSystem.transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDamage", theDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
The code for the enemy is:
#pragma strict
var Health = 100;
function Update ()
{
if (Health <= 0)
{
Dead();
}
}
function ApplyDamage (theDamage : int)
{
Health -= theDamage;
}
function Dead()
{
Destroy (gameObject);
}