- Home /
How Do I rotate a Object Based On the player's location?
So I'm trying to rotate a certain object based on the location of the player, I tried to do this, object.transform.rotation = player.transform.position, but this gives me a error saying "cannot implicity convert type UnityEngine.Vector3 to UnityEngine.Quaternion, does anyone know how to fix this? Thanks in advance
Object.transform.rotation = Quaternion.Euler(player.transform.position)
Quaternion.Euler takes in a Vector3 as the argument but eulerangles are only positive i think. You should check out the Quaternion class in the documentation
This doesn't really make any sense - Quaternion.Euler takes in angles in degrees around the three main 3D axes, so feeding it a position wouldn't really give any useful result. If you want to use a position/vector for rotation you can use Quaternion.LookRotation to orient the rotation along the direction of the vector;
//This will make 'object' look at the player
object.transform.rotation = Quaternion.LookRotation (player.transform.position - object.transform.position);
$$anonymous$$y bad I forgot to mention, this should work for 3D but how do I make it work for 2d games? Thanks for the help though @Llama_w_2Ls and @Namey5
Hello, What exactly do you want the rotation to achieve? Do you want the object to keep facing the player, without moving the position of the object? Or you want the object to rotate around the player?
I want the object to stay in its place, but its rotation changes based on the players position, I'm currently doing this on 2d
Then use the transform.LookAt(). Artik2442 already answered this.
Answer by Artik2442 · Aug 22, 2020 at 05:48 PM
Use Transform.LookAt()
Like that: object.Transform.LookAt(player.transform.position);