Rotate Player with mouse
This guide moves a player forward and back as usual with the character strafing left and right. The character will rotate left and right when the mouse is moved left or right on the x axis.
https://www.youtube.com/watch?v=50aaq80giLM&ab\_channel=LearnICTNow
No change will occur when the mouse is moved up and down vertically.
Create a new scene and player object.
The player object should not have any code attached to it.
Position the camera slightly above an behind the player.
Create a new script PlayerMoveAndRotate
Attach the script to the player prefab.
Open the script.
This script assumes that you have a PlayerInput with default Move and Look actions included on the input action map.
Line 4 imports the InputSystem
Lines 8-10 create private variables for storing the player rotation input _rotate, the player movement input _movement and the character controller _cc.
There are two public variables to store the player speed and the rotation speed of the player on lines 12 and 13.
The Start method includes a line of code to assign the Character Controller component to the _cc variable.
Lines 21-23 have the input handline method for the player movement. This is the same as other input system movement scripts. It takes the x y input and stores it in a Vector2 object.
The OnLook function does similar but it only needs to store the x input so this is stored in the float _rotate.
Making our player rotate is relatively simple. This is shown on lines 46-48. We have the RotatePlayer() method and call this on line 33 in the update function.
We have this method to make the Update method easier to understand and to make it easier to reuse the method at other points if needed.
Line 47 of the RotatePlayer() method will rotate the transform of the game object the script is attached to.
We use Vector3.up to rotate around the y (vertical axis), think spinning on a pole. We then multiply this by the _rotate input to get the direction to rotate the player. This is then multiplied by the rotation speed in RotationSpeed and the elapsed time since the last frame with Time.deltaTime.
Finally we need to move the player. This is where there are some changes to the basic movement from prior scripts.
This is because when we are rotating the player the direction the player is facing is changing. We need to move the player towards the new front of the player. not the original one.
Lines 36-44 contain the player movement. This is called on line 32 of the Update method.
Line 37 creates a new Vector3 for player movement and assigns the x and y movement values to the x and z coordinates.
On line 39 re multiply the z and x values of this Vector3 by the transform.forward and transform.right respectively. This sets the transform to the correct direction for the player.
On line 41 we move the character controller. We pass the Move method the Vector3 to move the player towards. This is our forward vector multiplied by our speed added to our left/right/strafe vector multiplied by our strafe speed.
Finally on Line 43 we apply gravity to our player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMoveAndRotate : MonoBehaviour
{
private float _rotate;
private Vector2 _movement;
private CharacterController _cc;
public float PlayerSpeed = 5;
public float RotationSpeed = 75;
// Start is called before the first frame update
void Start()
{
_cc = GetComponent<CharacterController>();
}
void OnMove(InputValue iv) {
_movement = iv.Get<Vector2>();
}
void OnLook(InputValue iv) {
_rotate = iv.Get<Vector2>().x;
}
// Update is called once per frame
void Update()
{
MovePlayer();
RotatePlayer();
}
void MovePlayer() {
Vector3 movement = new Vector3(_movement.x, 0.0f, _movement.y);
movement = transform.forward * movement.z + transform.right * movement.x;
_cc.Move(movement * Time.deltaTime * PlayerSpeed);
_cc.Move(Physics.gravity * Time.deltaTime);
}
void RotatePlayer() {
transform.Rotate(Vector3.up * _rotate * RotationSpeed * Time.deltaTime);
}
}