So I am creating a 2D project. I want my 2D sprite (a worm) to rotate in the direction of a specific point (eg a football sprite) in my game plane. I tried using LookRotation
but the problem is the worm is not facing perpendicular to the camera so it is not visible in 2D view. My game is on the X/Y plane.
It looks like this when I run it; Image 1 (Notice that the worm is not visible?)
This is the 3D view; Image 2
And this is the code;
Vector3 relativePos = target.transform.position - worm.transform.position;
Quaternion rotation = Quaternion.LookRotation (relativePos);
worm.transform.rotation = rotation;
Also, changing the upward direction, like setting it to vector3.forward
is not working. Many thanks.
If you just wan't to rotate the worm alone to face the target I will assume that you only want it to rotate in the Z axis. I copied your code, changed it to only follow the Z axis and it kinda looks at the target. I don't if this is the behaviour you want but if it is here is the code.
Vector3 relativePos = target.transform.position - worm.transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
rotation.x = worm.transform.rotation.x;
rotation.y = worm.transform.rotation.y;
worm.transform.rotation = rotation;
Here is an example of the behaviour with a cube facing a ball.