I’ve searched and searched and searched and I can’t seem to find any solution that works(Atan2, Quaternions, translate). And I’m becoming frustrated as this is a very simple concept yet Unity seems to not be able to comprehend.
I need a 2D sprite(Top Down, so u see player head, shoulders, and feet when they move) to look at the mouse… on the Z axis. I got it working perfectly fine on the normal 2D layout. But Unity is giving me hell trying to flip 90 degrees on the X(so player moves in X/Z not X/Y)
Any help?.. please…
All this is because Unity also doesn’t allow you to have vertical terrains. I’m using a 3d terrain as a 2d Map because I can paint textures on it instead of going with a blocky tilemap.
using UnityEngine;
public class LookAtMouseXZ : MonoBehaviour {
public Camera camera;
private Plane plane = new Plane(Vector3.up, Vector3.zero);
private Quaternion rotationOffset = Quaternion.Euler( 90, -90, -90 );
// Use this for initialization
void Start ()
{
if ( camera == null )
camera = Camera.main;
}
// Update is called once per frame
void Update ()
{
Ray ray = camera.ScreenPointToRay( Input.mousePosition );
float distance;
if( plane.Raycast( ray, out distance ) )
{
Vector3 target = ray.GetPoint( distance );
transform.rotation = Quaternion.LookRotation( target - transform.position, Vector3.up ) * rotationOffset;
}
}
}