- Home /
Question by
Yukken · Nov 08, 2017 at 02:07 PM ·
quaternionquaternion.lookrotation
I need to get a rotation from two transform position
let's say I have two objects a and b and I know their transform. I need to instantiate an object that'll face the same direction as the direction from object a to b .How do I get that rotation? In my game(it's 3d ) y is the upwards direction. I have tried but I can't seem to wrap my head around quaternions.
Comment
So if object A is facing object B, just set the rotation of the new instantiated object to equal the rotation of object A.
Best Answer
Answer by Hellium · Nov 08, 2017 at 03:40 PM
GameObject go = Instantiate( ... ) ;
Vector3 aPosition = a.GetComponent<Transform>().position ;
Vector3 bPosition = b.GetComponent<Transform>().position ;
Vector3 direction = bPosition - aPosition ;
go.GetComponent<Transform>().rotation = Quaternion.LookRotation(direction, Vector3.up);
Your answer
![](https://koobas.hobune.stream/wayback/20220612142551im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to aim an object at another object 1 Answer
Quaternion LookRotation 2 Answers
Limit Rotation 1 Answer
Quaternion LookRotation issue 0 Answers
Turret rotation with a pivot? 1 Answer