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?
1 Answer
1Please 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);
}
}
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.
– AlejandroGorgalAlso, if this is the actual code you're using, use a lower-case 'd' on var distance : float;
– clunk47Please keep us posted on a resolution.
– clunk47