- Home /
Setting Vector3.angle between two gameObjects
So here is the question: I am able to get an angle between two gameObjects by using Vector3.angle
public Transform target;
void Update()
{
Vector3 targetDir = target.position - transform.position;
float angle = Vector3.Angle(targetDir, transform.forward);
}
but the real problem is that I want to set this angle to another two gameObjects which are at different positions and angles in world space...........
Thank You
(I am sorry but i was not able to upload images because i am getting a disgusting error in unity answers which says - Error parsing the uploaded file.)
float angle = Vector3.Angle(targetDir, transform.forward);
Well this is actually angle between transform.forward and the other game object. And you cannot have angles between two objects. You need three objects.
However if you need to set the angle between your current gameObject and
then that can be done. Tell me if that's what you need.another two gameObjects
Answer by Chinmay_Gawande · Jun 21, 2019 at 10:34 PM
@ShamimAkhter Yes, I know that it is the angle between transform.forward of the gameobject at which the script is attached and the other one. I want the same angle between the 3rd gameobject's transform.forward with 4th gameobject. I know it seems complicated but I have a graphical representation of the same but I am not able to upload it due to the error.
Answer by ShamimAkhter · Jun 21, 2019 at 10:57 PM
Ok. You need to rotate your 3rd_gameObject so as to make the angle between its forward and 4th_gameObject equals to "angle".
Lets find the rotation axis. You can do it using: Vector3.cross(3rd_gameObject.transform.forward, targetDir); where targetDir will be (3rd_gameObject.position - 4th_gameObject.position).
Then rotate your 3rd_gameObject: 3rd_gameObject.transform.rotation = Quaternion.AngleAxis(angle, rotationAxis)
@ShamimAkhter it would be great if you will share it !