I have two objects in the scene, a cube and a cylinder (The player). I am casting a ray from the cubes transform.position to the players transform.position. Using that raycasts information, I need to cast another ray from the players transform.position, in the same direction as the first raycast, but only in the Y axis (Players horizontal plane). Is there an easy way to implement this?
Thanks in advance, hope the image is easy to understand. (Red = box, blue = player, yellow = first ray, orange = 2nd ray)
Given the direction Vector3 you are using to make your Ray, you can always flatten it just by setting the .y component to zero, then using it to make another Ray.
Oh wow, Thanks Kurt. I’ll try it out.
One extra thing to note: this won’t matter if you’re just using that Vector as the direction for a new Ray, but if you need it to be precisely length of 1.0 (such as for accurate speed settings), remember to just call .Normalize() on it after you set the .y to zero.
Hi Kurt, I tried casting the second ray as you have said and it works great on flat surfaces. But I still do have problems with it since I’m also using it for when moving on spheres and others surfaces. When I cast a ray with the .y set to zero, I noticed that the ray is rotating on the .y axis of the world space instead of the .y axis of the character (Who may be upside down or any other orientation)
Is there a way to get around this?
I believe this is your sauce:
https://math.stackexchange.com/questions/1023548/flatten-3d-vectora-so-its-perpendicular-to-vectorb
See answer 1.
The dot notation is vector dot, which is available in Unity as Vector3.Dot()
The answer I gave you is just the special case when 100% of the (b dot a) / (a dot a) term lies entirely on the y axis and is equal to the y axis. I think you can just normalize a before taking the b dot a then ignore the a dot a term, but don’t quote me because I haven’t had my coffee yet.
