- Home /
Question by
steveo1357 · Dec 02, 2013 at 04:17 AM ·
raycastvector3variabledistance
Distance Variable Won't Change. What's Wrong With My Script?
When I play my game and click the mouse button the distance variable should change, but it stays at zero. What am I doing wrong?
wha.jpg
(128.2 kB)
Comment
That looks like the damage script from Brackey's tutorial, you need to have some object with a collider on your scene for it to actually hit something.
Also, if this is the actual code you're using, use a lower-case 'd' on
var distance : float;
Best Answer
Answer by clunk47 · Dec 02, 2013 at 04:26 AM
Please be more specific. Is this an FPS? Are you trying to click on an object using the mouse cursor or the center of the camera's view??? If the distance variable isn't changing, you must not be moving. If you're not moving, and are clicking on different objects at different distances, you need to use a ray.
#pragma strict
var distance : float;
var hit : RaycastHit;
var ray : Ray;
function Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit) && Input.GetButtonDown("Fire1"))
{
distance = Vector3.Distance(transform.position, hit.transform.position);
print(distance);
}
}