Search code examples
unity-game-engineraycasting

Unity Player movement wasd and player rotates to face mouse


Hi all I am new to unity. I am trying to make a plane with a character controller that moves around and when you move you mouse the player faces that direction. I has success with using lookat with arrow keys but with a mouse im very close just one bug which I can see but am not sure how to fix.

Its a 3d Enviroment, camera is at an angle behind the player. So far I have: made a "new plane" I raycast to it all works but the player can lean over and flip because my raycast is hitting the floor, from using Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

is there a way I can use raycast but set the player to stay upright and the line cast stay at a set point not go to the floor ?

float dist;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        if (plane.Raycast(ray, out dist))
        {
            Vector3 point = ray.GetPoint(dist);
            transform.LookAt(point);
            Debug.DrawLine(transform.position, point, Color.red);
        }

I expect player to not look up and down and the player to rotate to look at the mouse position.

Camera is at a fixed angle so its 3d


Solution

  • The clamp function was good to stop the player flipping.

    But I used a detection in the end to get weapon height, then on jump adjust to keep it straight.