0
votes

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

1
You mean you want the player to rotate based on the mouse angle? For example, it should not look Up or Down but look Left and Right?user11458208
@Saif I would like the player to move with wasd and mouse to be the direction it can shoot in. Player cant look up or down so it more the direction and not the Y angle Im guessing im using characterController = GetComponent<CharacterController>(); movement works jump works rotation to face mouse on 3d camera angle fails for meCommanderCalm
Do you know the function Mathf.Clamp()? Its where you can give limit for your rotation angle so that the player would not go beyond an angle (positive and negative). I am not infront of a computer right now but do look at Mathf.Clamp() function.user11458208
@Saif Thanks I will take a look. The Player can rotate 360 to shoot as they will be in the middle of the screen just up and down I dont needCommanderCalm
then Mathf.Clamp() function works for your logic. It contains min and max values that you can set for Y axis angle.user11458208

1 Answers

0
votes

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.