Home > OS >  Smooth out movement with new Input system Unity
Smooth out movement with new Input system Unity

Time:07-06

I'm making a platformer controller using the new Input System and trying to control Smooth movement for jumping and moving... I'm pretty sure I can lerp, slerp or SmoothDamp over the values. The problem now is I'm using a Player Input Component and Unity Events to control the Move and Jump.... And I cannot figure out where/how to add the Vector2.SmoothDamp. I think if I move everything to update it could work but then doesn't that defeat the purpose of unity events? Any advice?

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

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private Rigidbody2D rb;

    [SerializeField] private Transform groundCheck;
    
    [SerializeField] private LayerMask groundLayer;
    private float _horizontal;
    
    private const float Speed = 8f;
    
    private float jumpingPower = 16f;
    private bool _isFacingRight = true;

    private const float GroundedRadius = 0.2f;

    // Update is called once per frame
    void Update()
    {
        rb.velocity = new Vector2(_horizontal * Speed, rb.velocity.y);
        
        if (!_isFacingRight && _horizontal > 0f || _isFacingRight && _horizontal < 0f)
        {
            Flip();
        }
    }

    public void Move(InputAction.CallbackContext context)
    {
        _horizontal = context.ReadValue<Vector2>().x;
    }
    
    public void Jump(InputAction.CallbackContext context)
    {
        if (context.performed && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
        }

        if (context.canceled && rb.velocity.y > 0f)
        {
            var velocity = rb.velocity;
            var gravity = rb.gravityScale;
            velocity = new Vector2(velocity.x, -(velocity.y * gravity * 2f));
            
            rb.velocity  = velocity;
        }
    }
    private bool IsGrounded()
    {
        return Physics2D.OverlapCircle(groundCheck.position, GroundedRadius, groundLayer);
    }

    private void Flip()
    {
        _isFacingRight = !_isFacingRight;
        var transform1 = transform;
        Vector3 localScale = transform1.localScale;
        localScale.x *= -1f;
        transform1.localScale = localScale;
    }


}

ui

CodePudding user response:

So, here's what I see:

rb.velocity = new Vector2(_horizontal * Speed, rb.velocity.y);

You have this line which sets the final "real" velocity. Everything directly affects _horizontal (the direction I presume you're trying to dampen).

You could always set up an additional variable called "_horizontalFinal", use this in place of _horizontal when setting the rigidbody velocity, and add this line to the Update method:

_horizontalFinal = Mathf.Lerp(_horizontalFinal,_horizontal,Time.deltaTime*6);

Replace 6 with whatever Lerp speed you want. Hope this helps.

  • Related