Question by
Mrsoldier3201 · Mar 16, 2016 at 10:08 PM ·
playerobjectraycastingdistancelaser
Tell distance from player to an object using raycast?
I'm trying to find the distance between the player and an object it is looking at. I plan to draw visible laser between the objects, and need the distance. Here's what I have to far
Vector2 direction = Quaternion.AngleAxis(angle - 90, Vector3.forward) * Vector3.forward; //Setting the direction of the Ray
if (Physics.Raycast(transform.position, direction, 10))//Checking for any contact with an object
{
dist = hit.distance;
}
However dist = 0 no matter where the player is. Why?
Game is a TDS, in C#, Thanks!
Comment
$$anonymous$$aybe direction is calculated wrong ? Turn on Gizmos and check it if it's in the right direction:
Vector2 direction = Quaternion.AngleAxis(angle - 90, Vector3.forward) * Vector3.forward; //Setting the direction of the Ray
Debug.DrawLine(transform.position, direction, Color.red, 5.0f);
or checking the wrong axis
Vector2 direction = Quaternion.AngleAxis(angle - 90, Vector3.up) * Vector3.up; //Setting the direction of the Ray
Debug.DrawLine(transform.position, direction, Color.yellow, 5f);
Your answer
![](https://koobas.hobune.stream/wayback/20220612072125im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do i get the angle of two Vector 3's? 1 Answer
Raycast detect in object (problem) 0 Answers
Particles for player object? 0 Answers
How to activate "OnTriggerEnter" when Player ray hits an object collider? 1 Answer
What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 0 Answers