I have a ray cast script. When I press control it makes a ray in the editor. I have the ray go through the monster and doesn’t always lowers a monsters health. Only sometimes it does. Also if I get to close to the monster and touch him, his health goes down rapidly and I haven’t even fired a ray. What did I do wrong? Here is my code.
var distance : float = 30;
function Update ()
{
if(Input.GetButton("Fire1")){
var dir = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawRay(transform.position, dir * distance, Color.blue);
print("You fired a ray");
}
if(Physics.Raycast(transform.position, dir, hit, distance))
{
if(hit.collider.gameObject.tag == "Monster")
{
health.Health -= 5;
print("You Hit Him");
}
}
}