- Home /
Duplicate Question
How do I specify the position of one object relative to another object?
Let's say, I have a position variable
private Vector3 leaderPosition;
I want to specify another vector
private Vector3 followerPosition;
I want this followerPosition to be 5 meters away from leaderPosition and at an angle of 45 degrees behind him (diagonally behind him like a V formation).
How do I do that? How do I manually set "followerPosition" to something like
followerPosition = leaderPosition + (something);
Answer by robhuhn · Sep 24, 2013 at 07:16 AM
To find the position you would create a vector pointing behind the leader (negative forward):
Vector3 offset = -leader.transform.forward; //1m behind
Multiply that vector with the distance
offset *= 5; //5m behind
Rotate the offset
offset = Quaternion.AngleAxis(45, leader.transform.up) * offset; //45 degrees around the leaders up vector
All together
Vector3 offset = Quaternion.AngleAxis(45, leader.transform.up) * -leader.transform.forward * distance;
transform.position = leader.transform.position + offset;
Note: The order of multiplication is important here.
Answer by ScotchBunny · Sep 24, 2013 at 07:20 AM
If you use
followerPosition = leaderPosition + (something);
your follower will be in the same world direction in relation to your leader; the follower may always be south-east of the leader, for example. That is unless you take the leader orientation into your calculations.
The easier way would be to use Transform.TransformPoint. That should take care of it for you.
Follow this Question
Related Questions
Translate Vector3 by Vector3 1 Answer
Vector relative to Object coordinates 1 Answer
Relative Rotation 1 Answer
Aim Down Sights Positioning Problem 1 Answer