(How to ?) SmoothDamping the new Input System

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… :sweat_smile:

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;
    }
}


Hi,

Did you test your code? And are you happy with the result? If so, I would claim that it is doing something. :wink:

Yes i tried it and it seem i’m getting some smoothing on x,y Current but without any significant improvement. The z axis is not affected by any means…I think a nice semplification would be to smooth out only the Roll (z)axis where we are getting only -3 or 3 values. Also tried setting the 2D vector mode to Analog without any result.

Possibile fixes(?):

  • Smooth out out the “controlRollFactor” variable
  • Switch back to standard input system
  • Buy a joypad xD

Why do you want to smooth the input values? What is the problem you would like to solve by doing that? Maybe there is another solution.

Instead of overriding the position, you could, for example, use the AddRelativeForce method of the Rigidbody class. If the input values are “unpredictable”, you could check if the values are higher, lower or equal 0 and use -1, 1 and 0 respectively for your movement.

The MovePosition method of the Rigidbody method could also be interesting.

Proper way to do something with input value in new input system is Processors | Input System | 1.3.0

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms