Player Controls (New Input System)

Mouse Look

You don’t have access to this lesson
Please register or sign in to access the course content.

Below is a code example for how to setup a mouse look on a camera object.

speedH and speedV are the speed at which the camera will rotate horizontally and vertically.

With the default input actions a Look binding is created.

This allows us to use an OnLook function[1] call.

As this is bound to the mouse (or right stick) a 2D vector is accepted.

This is handled in the same way that movement is for the player movement.

The difference is that this is used to change the camera viewpoint.

The camera is transformed in the Update() function.

You need[3] to add a PlayerInput component[2] to the Camera object.

using System.Collections; 
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class CameraScript : MonoBehaviour
{

    public float speedH = 2.0f;
    public float speedV = 2.0f;

    private float yaw = 0.0f;
    private float pitch = 0.0f;

    private float movementX, movementY;

    // Use this for initialization
    void Start()
    {

    }

    void OnLook(InputValue movementValue)
    {
        Vector2 movementVector = movementValue.Get<Vector2>();

        movementX = movementVector.x;
        movementY = movementVector.y;
    }


    // Update is called once per frame
    void Update()
    {

        yaw += speedH * movementX;
        pitch -= speedV * movementY;

        transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);

    }
}
Terms definitions
2. component. The three strands of Technology in the NZC (Technological Practice[4], Technological Knowledge, and Nature of Technology) – together with the six specialist knowledge and skills strands for levels 6, 7, and 8) – support[6] the development of students’ technological literacy[5]. Each of these strands is broken down into smaller groupings (or substrands) called “components”. For […]
3. need. An identified requirement of a person, group, or environment[7]. A need is identified from an issue[8] and sits within a context[9]. Technological practice[4] can be undertaken in an attempt to meet an identified need.
Terms definitions
1. function. Below is a code example for how to setup a mouse look on a camera object. speedH and speedV are the speed at which the camera will rotate horizontally and vertically. With the default input actions a Look binding is created. This allows us to use an OnLook function call. As this is bound to […]
2. component. Below is a code example for how to setup a mouse look on a camera object. speedH and speedV are the speed at which the camera will rotate horizontally and vertically. With the default input actions a Look binding is created. This allows us to use an OnLook function call. As this is bound to […]
3. need. Below is a code example for how to setup a mouse look on a camera object. speedH and speedV are the speed at which the camera will rotate horizontally and vertically. With the default input actions a Look binding is created. This allows us to use an OnLook function call. As this is bound to […]
4. Technological Practice. Below is a code example for how to setup a mouse look on a camera object. speedH and speedV are the speed at which the camera will rotate horizontally and vertically. With the default input actions a Look binding is created. This allows us to use an OnLook function call. As this is bound to […]
5. technological literacy. Below is a code example for how to setup a mouse look on a camera object. speedH and speedV are the speed at which the camera will rotate horizontally and vertically. With the default input actions a Look binding is created. This allows us to use an OnLook function call. As this is bound to […]