Unity - Gameobject look at mouse

后端 未结 1 1792
长发绾君心
长发绾君心 2021-01-18 19:10

I\'ve ran into a problem.

The basic setup I have right now, two objects: my camera, and my player object.

The player moves via Transform on WASD, and is supp

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 19:48

    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.

    0 讨论(0)
提交回复
热议问题