Hi! Im trying to learn the new Input system and i got movement down, but i cant seem to figure out mouse look. its a 1st person controller by the way. This is the script i got:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public int walkSpeed;
public int runSpeed;
public float sensitivityX;
public float sensitivityY;
int moveSpeed;
CharacterController controller;
Vector3 moveDirection;
bool jump;
bool run;
Transform cam;
Transform player;
float lookX;
float lookY;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
player = transform;
cam = Camera.main.transform;
}
// Update is called once per frame
void Update()
{
Move();
Look();
}
void Look()
{
player.rotation = Quaternion.Euler(0, lookX, 0);
cam.rotation = Quaternion.Euler(lookY, 0, 0);
}
void Move()
{
if (run)
{
moveSpeed = runSpeed;
}
if (!run)
{
moveSpeed = walkSpeed;
}
controller.Move(moveDirection * moveSpeed * Time.deltaTime);
}
public void OnMove(InputAction.CallbackContext context)
{
moveDirection = new Vector3(context.ReadValue<Vector2>().x, 0, context.ReadValue<Vector2>().y);
}
public void OnLook(InputAction.CallbackContext context)
{
lookX = context.ReadValue<Vector2>().x;
lookY = context.ReadValue<Vector2>().y;
}
public void OnJump(InputAction.CallbackContext context)
{
jump = context.started;
}
public void OnRun(InputAction.CallbackContext context)
{
run = context.performed;
}
}
On the player I have this script attached, a player Input Component, with unity components that attach to each of the on look on jump and on run functions. What this script does now is when i move the mouse it moves but then quickly shoots back to the original position so it doesnt actually look around. Thanks for the help in advance!!
Didn’t you want increase or decrease the rotation by whatever the lookx/y contains? The Input System will go back to zero on these values as soon as you don’t push the joystick/move the mouse. You need incremental rotation.
@djc51401 You shouldn’t be using Time.deltaTime in OnLook(). The delta value tells you how much the mouse has moved since the last time the event was triggered, so it doesn’t depend on how long that interval was.
Besides, Time.deltaTime is about how long it has been since the last frame, and your event handlers (such as OnLook()) are not tied to the frame rate at all. They are called separately by the input system whenever there is a user input.
@djc51401 thanks for the post. This helped me with some new Input System stuff.
In regards to your comment about the Quaternion making things glitchy: It looks like you were trying to set the x and y values of the Quaternion as if they were the x and y Euler values. You fixed this in the last version you posted, but just in case someone else is coming along and reading this, Quaternions are internally very different from the Euler angles we usually think of for rotations. In a Quaternion, x, y, and z are imaginary numbers (i.e., multiples of i, which is the square root of -1), and w is a real number. It’s a bizarre way to store rotations, but it works really well in matrix multiplication (and the order of the matrix multiplication determines whether it’s a local or global rotation), and it works because of the interesting circular nature of multiplying i:
i * i = -1
-1 * i = -i
-i * i = 1
1 * i = i
i * i = -1 …and so on
When I’m teaching, I usually tell my students to not worry about how Quaternions work and to just use Euler angles instead, as you have done.