- Home /
The question is answered, right answer was accepted
How to place an object in the direction of a raycast
Hi there, I am trying to place a transform object in the position of the end point of a ray. As you can see that the yellow line was drawn using the following code: Debug.DrawRay(ahead.position, (temp.position - transform.position).normalized * 4, Color.yellow);
Correct me if I am wrong but the line above draws a ray from the ahead.position and points in a direction which is (temp.position-transform.position). I want the "temp2" object to be placed at the end of the ray that I just drew. I want it to appear at the position of the white "X" in the picture.
Answer by Pennywise881 · Dec 30, 2017 at 02:28 PM
Ok so I got it. Its actually quite simple and I realized how to do it just a couple of minutes after posting the question :p. All you have to do is take a vector which points from the position of the capsule to the "temp" vector, scale it and then add that to the "ahead" vector. In the end the code looked something like this:
Vector3 someVector=ahead.position + (temp.position - transform.position).normalized * someScale;
temp2.positon=someVector;
Thats it!
Follow this Question
Related Questions
Door only opening once on Raycast 1 Answer
Boxcast vs Raycast Oddities 1 Answer
Stop the lasers from keep bouncing 1 Answer
Raycast detects box collider, but not capsule collider 0 Answers
Click to move script help 1 Answer