简体   繁体   English

使用新的输入系统 Unity 平滑移动

[英]Smooth out movement with new Input system Unity

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.我正在使用新的输入系统制作一个平台游戏控制器,并尝试控制平滑运动以进行跳跃和移动......我很确定我可以对这些值进行 lerp、slerp 或 SmoothDamp。 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.现在的问题是我正在使用播放器输入组件和 Unity 事件来控制移动和跳跃......而且我无法弄清楚在哪里/如何添加 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;
    }


}

用户界面

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).一切都直接影响 _horizo​​ntal (我认为你试图抑制的方向)。

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:您总是可以设置一个名为“_horizo​​​​ntalFinal”的附加变量,在设置刚体速度时使用它代替_horizo​​ntal,并将此行添加到Update方法中:

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

Replace 6 with whatever Lerp speed you want.用你想要的任何 Lerp 速度替换 6。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM