I’ve got the following script that draws a line between two objects, one moveable (posChar) and one static (posFloor). I’m trying to use it to calculate the distance between these two objects, but the value of the float “distance” from the following code is always returning zero, regardless of where I move my character. Any advice on what I’m doing wrong would be appreciated. Thanks.
public GameObject characterPos;
public GameObject floorPos;
Vector3 posChar;
Vector3 posFloor;
public float distance;
void Update()
{
posChar = characterPos.transform.position;
posFloor = floorPos.transform.position;
Debug.DrawLine(posChar, posFloor);
RaycastHit hit;
if(Physics.Linecast(posChar, posFloor, out hit, LayerMask.NameToLayer("Ground")))
{
distance = hit.distance;
}
Debug.Log(distance);
}