Unity – Gameobject look at mouse

I’m not sure If I understand what you are trying to do. If you are trying to do something similar to the game “Dead Nation”, then I would suggest something like this:

MouseLook.cs

void Update()
{
    Vector3 mouse = Input.mousePosition;
    Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(new Vector3(
                                                        mouse.x, 
                                                        mouse.y,
                                                        player.transform.position.y));
    Vector3 forward = mouseWorld - player.transform.position;
    player.transform.rotation = Quaternion.LookRotation(forward, Vector3.up);
}

If you want the camera to move and rotate along with the player then just make the camera a child of the player object.

Leave a Comment

error code: 523