- Home /
Linecast not returning distance
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);
}
Answer by kat0r · Feb 10, 2014 at 11:05 PM
You need to use
if(Physics.Linecast(posChar, posFloor, out hit, 1 << LayerMask.NameToLayer("Ground")))
instead of
if(Physics.Linecast(posChar, posFloor, out hit, LayerMask.NameToLayer("Ground")))
If you just want the distance, why not use
Vector3.Distance(a,b)
?
Thanks.
This worked, but the value of "distance" is now slowly decreasing by roughly 0.004f every frame whilst my player object and the floor object are both stationary. I found that this also happened using the Vector3.Distance(a, b) method, which I was originally using (as you've suggested). It's the reason behind my trying this linecast method.
Do you have any idea why the value of "Distance" would decrease every frame by this amount (0.004f) whilst the two objects are stationary? The value seems to reset itself when I move the player, but for my purposes I need a precise and stable measurement between the player and the floor at all times.
If both objects really are stationary, this cannot happen (there may be some "flickering" because floats are not exact, but approximations). If distance is decreasing, so would the distance your linecast gets, btw.
I would first check WHY those objects are see$$anonymous$$gly getting closer, without seeing your code I would assume it is because of physics. (like gravity)
You were right. It was to do with the fact that "Use Gravity" was ticked on my character's Rigidbody. I unchecked that and the problem stopped. Strange. Thanks for the help!
Answer by karlhulme · Feb 10, 2014 at 11:10 PM
Hi,
Physics.LineCast will tell you if a line drawn between the two points hits a collider, and if it does, the distance to that collider. I'm guessing one of two things is happening…
The Physics.LineCast is returning false, so distance is never being set, hence Debug.Log shows zero.
The Physics.LineCast is returning true because you've got a collider on the ground, which is intersecting with posFloor - hence the distance is genuinely zero.
If you just want the distance between two points, consider Vector3.Distance.
Hope this helps.
Your answer
Follow this Question
Related Questions
How to increment the number by 1's to get distance traveled? 0 Answers
iOS Lag From Updating Score 0 Answers
calculating vertical distance 1 Answer
My look at script not working? 0 Answers
is it possible change picture or video content once published? 0 Answers