As quoted by YRTV in this post the new input system seem to act very snappy when reading keyboards input.
I’m trying to SmoothDamp these x / y rotation values and here’s my code.
I’m actually unsure if this is doing something or not at all…
sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[SerializeField] private InputAction move = new InputAction("Move", InputActionType.Value, null, null, null, "Vector2Control");
[SerializeField] private float controlSpeed = 20f;
[SerializeField] private float xRange = 20f;
[SerializeField] private float yRange = 13f;
[SerializeField] private float positionPitchFactor = -2.5f;
[SerializeField] private float positionYawFactor = -0.5f;
[SerializeField] private float controlPitchFactor = -1.5f;
[SerializeField] private float controlRollFactor = -20f;
private Vector2 movementInput;
[SerializeField]float smoothTime = 0.3f;
[SerializeField]float xVelocity = 0f;
[SerializeField]float yVelocity = 0f;
[SerializeField]float xCurrent = 0f;
[SerializeField]float yCurrent = 0f;
private bool isMoving;
private void OnEnable()
{
move.Enable();
}
private void OnDisable()
{
move.Disable();
}
private void Awake()
{
move.AddBinding("<Gamepad>/leftStick");
move.AddCompositeBinding("2DVector")
.With("Up", "<Keyboard>/w")
.With("Down", "<Keyboard>/s")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d");
move.started += ctx => OnMoveStarted();
move.performed += ctx => OnMove(ctx);
move.canceled += ctx => OnMoveCancel();
}
void Update()
{
ProcessInputState();
}
private void ProcessInputState()
{
if (isMoving)
{
float newX = Mathf.SmoothDamp(xCurrent, movementInput.x, ref xVelocity, smoothTime); //Damping
float newY = Mathf.SmoothDamp(yCurrent, movementInput.y, ref yVelocity, smoothTime); //Damping
xCurrent = newX;
yCurrent = newY;
Vector2 movementVector = movementInput * (controlSpeed * Time.deltaTime);
newX = Mathf.Clamp(transform.localPosition.x + movementVector.x, -xRange, xRange); //Clamping
newY = Mathf.Clamp(transform.localPosition.y + movementVector.y, -yRange, yRange); //Clamping
transform.localPosition = new Vector3(newX, newY, transform.localPosition.z);
float pitch = transform.localPosition.y * positionPitchFactor + movementInput.y * controlPitchFactor;
float yaw = transform.localPosition.x * positionYawFactor;
float roll = movementInput.x * controlRollFactor;
transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}
}
void OnMoveStarted()
{
isMoving = true;
}
void OnMove(InputAction.CallbackContext ctx)
{
movementInput = ctx.ReadValue<Vector2>();
}
void OnMoveCancel()
{
movementInput = Vector2.zero;
isMoving = false;
}
}