- Home /
Rotate one vector towards another but keeping it on the same plane
So I think this should be easy but I don't have terminology to google the right thing. So imagine one object is on the ground and the other is flying I want to find a direction toward the flying object but so the ground object keeps moving on the same Y coordinate. So I found one way to do this
var dir= transform.InverseTransformDirection(dir);
dir.y = 0;
dir = transform.TransformDirection(dir);
I can also maybe project direction on a plane defined by forward and right vectors of the ground object. But I feel like there should be an easier solution any ideas?
Answer by Bunny83 · Apr 15, 2020 at 05:40 PM
Well to get the same behaviour as your code you can just use Vector3.ProjectOnPlane like this:
dir = Vector3.ProjectOnPlane(dir, transform.up);
This will project "dir" onto the horizontal plane relative to the given transform. Keep in mind that both, your code and this one, do not rotate the vector but project it onto the plane. That means at steeper angles the resulting vector is getting shorter. So you might want to normalize it after the projection. If you want it to have the same length as before the projection you just have to remember the magnitude before you project the vector. After the projection you can normalize the vector and multiply it by your old magnitude.
Your answer
Follow this Question
Related Questions
Rotate an object until its x axis lies in a plane perpendicular to a given vector... 1 Answer
How to check if distance between two objects is getting smaller? 1 Answer
How can I get back a hit? 1 Answer
2D Ball Bouncing Angle Problem (Without Rigidbody) 0 Answers
Determine if an object passes a point along a vector 1 Answer