Movement using new input system

Hey there,

Maybe someone like me struggles with new version of Unity - I decided to start this project on Unity 2019.4.10f1 version. Old standard asset pack is kinda old and there so many new things one of them is new input system, there are not many good tutorials atm but still I figured out some basics and I tried to create control system using new Input. I had to add some smoothing to raw values from new input system because I could not find something like sensitivy and gravity :thinking:

Here is my code:

using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerControler : MonoBehaviour
{
    [SerializeField] float maxYRange;
    [SerializeField] float minYRange;
    [SerializeField] float xRange;
    [SerializeField] float ySpeed;
    [SerializeField] float xSpeed;
    [SerializeField] float positionPitchFactor = -5f;  // variable controlling the spaceship pitch based on current spaceship position on Y axis
    [SerializeField] float controlPitchFactor = -5f;  // variable controlling the additional spaceship pitch while pressing W/S or UP/DOWN arrow keys
    [SerializeField] float positionYawFactor = 5f;  // variable controlling  the spaceship yaw based on current spaceship position on X axis
    [SerializeField] float controlYawFactor = 5f;  // variable controlling the additional spaceship yaw while pressing A/D or LEFT/RIGHT arrow keys
    [SerializeField] float controlRollFactor = -10f;  // variable controlling the spaceship roll while pressing A/D or LEFT/RIGHT arrow keys
    [SerializeField] bool applySensitivyInput;
    [SerializeField] float inputSensitivy;
    [SerializeField] InputAction inputAction;
    private Vector2 currentInputProgress;
    private Vector3 startingRotation;

    private void OnEnable()
    {
        inputAction.Enable();
    }

    private void OnDisable()
    {
        inputAction.Disable();
    }

    private void Start()
    {
        transform.localRotation = Quaternion.Euler(10, 0, 0);
        startingRotation = new Vector3(transform.localRotation.x, transform.localRotation.y, transform.localRotation.z);
    }

    private void Update()
    {
        // Apply sensitivy smoothing to input system value
        if (applySensitivyInput)
        {
            SmoothInput();
        }
        ProcessTranslation();
        ProcessRotation();
    }

    private void ProcessTranslation()
    {
        // Apply horizontal movement if input value is not 0
        if (CurrentVector2Value().x != 0)
        {
            float currentX = transform.localPosition.x;
            float clampedX = Mathf.Clamp(currentX + ThisFrameOffset(CurrentVector2Value().x, xSpeed), -xRange, xRange);
            transform.localPosition = new Vector3(clampedX, transform.localPosition.y, transform.localPosition.z);
        }
        // Apply horizontal movement if input value is not 0
        if (CurrentVector2Value().y != 0)
        {
            float currentY = transform.localPosition.y;
            float clampedY = Mathf.Clamp(currentY + ThisFrameOffset(CurrentVector2Value().y, ySpeed), minYRange, maxYRange);
            transform.localPosition = new Vector3(transform.localPosition.x, clampedY, transform.localPosition.z);
        }
    }

    private void ProcessRotation()
    {
        float pitch = transform.localPosition.y * positionPitchFactor + CurrentVector2Value().y * controlPitchFactor;
        float yaw = transform.localPosition.x * positionYawFactor + CurrentVector2Value().x * controlYawFactor;
        float roll = CurrentVector2Value().x * controlRollFactor;

        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
    }

    private void SmoothInput()
    {
        currentInputProgress = Vector2.Lerp(currentInputProgress, inputAction.ReadValue<Vector2>(), inputSensitivy * Time.deltaTime);
    }

    private Vector2 CurrentVector2Value()
    {
        if (applySensitivyInput)
        {
           return currentInputProgress;
        }
        else
        {
            return inputAction.ReadValue<Vector2>();
        }
    }

    private float ThisFrameOffset(float vectorValue, float speed)
    {
        return vectorValue * speed * Time.deltaTime;
    }
}

And here is result:

2 Likes

What was the hardest part in creating the input system?

The hardest part was find some useful tutorial which was uodated for current state.

1 Like

Privacy & Terms