I have a raycast being sent out but the distance it returns is VERY inconsistent. sometimes I’ll be standing right infront of something and it’ll say 60 or something ridiculous, other times it will do the complete opposite and then sometimes it will say something that seems completely right. This is my code (please note there is a part in there that does not do anything at this current time).
#pragma strict
var dmg : int = 50;
var distance : float = 1.0;
var MaxDistance : float = 1.5;
function Update()
{
if (Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit, 100.0))
{
distance = hit.distance;
if (distance < MaxDistance)
{
hit.transform.SendMessage("Applydmg", dmg, SendMessageOptions.DontRequireReceiver);
}
}
}
}
Maybe the RayCast is finding a hit against an object that is 60m away from you? If you only want collisions inside a 1.5m range, why not set the last argument to 1.5f?
– Graham-DunnettAS the first two lines inside your Raycast() do: Debug.Log(hit.collider.name+", "+hit.collider.tag); Debug.DrawRay(transform.position, transform.forward * hit.distance); This will tell you what is hit, and the end of the raycast will tell you the hit position. Turn on Gizmos in the upper right corner of the Game window.
– robertbusorry where exactly do i put those two lines of code?
– ReafyBetween lines 14 and 15. You want it to be the first two lines inside the 'if' statement. So if the Raycast() hits something, these two lines will tell you 1) what was hit, and 2) where it was hit.
– robertbuoh i see. I did that and it seems that the ray isn't going straight.
– Reafy