Specific Game Mechanics

Rotate Player with mouse

You don’t have access to this lesson
Please register or sign in to access the course content.
Length: 30 minutes|Difficulty: Standard

This guide[6] moves a player forward and back as usual with the character[1] strafing left and right. The character will rotate left and right when the mouse is moved left or right on the x axis.

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[2] 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[1] Controller component[3] to the _cc variable[4].

Lines 21-23 have the input handline method for the player movement. This is the same as other input system[7] movement scripts. It takes the x y input and stores it in a Vector2 object.

The OnLook function[5] does similar but it only needs to store the x input so this is stored in the float[8] _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[9] to move the player. This is where there are some changes to the basic[10] 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);
    }
}
Terms definitions
1. character. This guide moves a player forward and bakc 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.
2. variables. “Containers” used to label and store data[18] in memory. The data can then be used throughout a program: * Integer[19] variables use only numbers and so can be used for calculations * String[20] variables are sequences of code that may contain numbers, letters, and other characters, and so cannot be used for calculations.
3. component. The three strands of Technology in the NZC (Technological Practice[11], Technological Knowledge, and Nature of Technology) – together with the six specialist knowledge and skills strands for levels 6, 7, and 8) – support[13] the development of students’ technological literacy[12]. Each of these strands is broken down into smaller groupings (or substrands) called “components”. For […]
4. variable. A placeholder for a piece of information that can change.
5. function. A named section of a computer program that performs a specific task. Functions help make code more efficient and reusable. They may take input parameters and produce output.
Terms definitions
1. character. Length: 30 minutes|Difficulty: Standard 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. No change will occur when the mouse is moved up and down vertically. Create a […]
2. variables. Length: 30 minutes|Difficulty: Standard 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. No change will occur when the mouse is moved up and down vertically. Create a […]
3. component. Length: 30 minutes|Difficulty: Standard 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. No change will occur when the mouse is moved up and down vertically. Create a […]
4. variable. Length: 30 minutes|Difficulty: Standard 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. No change will occur when the mouse is moved up and down vertically. Create a […]
5. function. Length: 30 minutes|Difficulty: Standard 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. No change will occur when the mouse is moved up and down vertically. Create a […]