Unity新输入系统:在某些情况下,玩家并不总是跳跃。

huangapple go评论65阅读模式
英文:

Unity New Input System: Player is not always Jumping In some occasions

问题

Unity新的输入系统在某些情况下不总是起作用。

我试图让我的玩家跳跃,但出现了问题,当我的玩家朝W+A方向行走时,我的玩家不会跳跃,同样当我的玩家朝W、W+A、W+D方向奔跑时也是如此。

我认为这与新的输入系统有关,可能是我必须更改的某些内容,或者我不知道的原因。

在我上面提到的情况下,它不会调试这条消息:

public void SetJump(InputAction.CallbackContext value)
{
    if (value.started) Debug.Log("I'm Ready to jump");
}

注意:我的玩家在其他情况下可以跳跃,只是在我上面提到的情况下不行。

注意:我的方法 IsGrounded 也完全正常运行,我进行了检查,甚至将我的玩家放在了平坦的地形上。

现在,我将展示我的整个代码以及我如何配置我的输入系统。

输入系统配置:

Unity新输入系统:在某些情况下,玩家并不总是跳跃。

Unity新输入系统:在某些情况下,玩家并不总是跳跃。

Unity新输入系统:在某些情况下,玩家并不总是跳跃。

Unity新输入系统:在某些情况下,玩家并不总是跳跃。

代码:

public class PlayerMovement : MonoBehaviour
{
    // ...(其余代码未翻译,仅提供关键信息)...
}

我的玩家具有 CharacterController 组件、胶囊碰撞器、附加的 Player Input System,并且方法正确链接到玩家。

英文:

Unity new input system is not always working in some situations.

I was trying to make my player jump but I got an Issue, when my player is walking in the direction W+A my player does not jump, also when my player is running in the directions W, W+A, W+D.

I think it has something to do with new input system, something that I must change or I don't know because.

In the situations I mentioned above it's not debugging the message

public void SetJump(InputAction.CallbackContext value)//usa o parametro pra recer resultado das action (nessa caso vetor2
{
    if (value.started) Debug.Log("I'm Ready to jump");

Note: my player is jumping in other situations just not on the situations I mentioned above.

Note: my method IsGrounded is also perfectly functioning I checked it, I even put my player on flat terrain.

Now, I will show my entire code, and how I configured my input system

Input system configuration:

Unity新输入系统:在某些情况下,玩家并不总是跳跃。

Unity新输入系统:在某些情况下,玩家并不总是跳跃。

Unity新输入系统:在某些情况下,玩家并不总是跳跃。

Unity新输入系统:在某些情况下,玩家并不总是跳跃。

Code:

public class PlayerMovement : MonoBehaviour
{
    [Header("Config Player")]

    [SerializeField] private float speed = 3f;
    [SerializeField] private float speedWhileRunningMultiplier = 1.3f;
    [SerializeField] private float gravity = -9.8f;
    [SerializeField] private float gravityMultiplier = 3f;
    [SerializeField] private float jumpForce = 9.8f;
    [SerializeField] private float smoothTime = 0.05f;

    [SerializeField] private bool isGrounded = false;

    private CharacterController controller;
    private Animator anim;

    private bool isWalking, isRunning = false;//for animation purposes

    private float currentSpeedRotating;
    private float verticalVelocity = 0f;
    
    private Vector2 movement2D;
    private Vector3 direction;

    private bool IsGrounded() => controller.isGrounded;

    private void Awake()
    {
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
    }

    void FixedUpdate()
    {
        ApplyMovement();
        ApplyRotation();
        ApplyGravity();
    }

    public void SetMovement(InputAction.CallbackContext value)
    {
        movement2D = value.ReadValue<Vector2>();
        direction = new Vector3(movement2D.x, 0, movement2D.y);
        direction.Normalize();
    }

    public void SetJump(InputAction.CallbackContext value)
    {
        if (value.started) Debug.Log("I'm Ready to jump");
        if (!value.started) return;
        if (!IsGrounded()) return;

        verticalVelocity += jumpForce;
    }

    public void SetRun(InputAction.CallbackContext value)
    {
        isRunning = value.performed ? true : false;
    }

    void ApplyMovement()
    {
        isWalking = direction.x != 0 || direction.z != 0;

        float currentSpeed = speed * (isRunning ? speedWhileRunningMultiplier : 1f);

        controller.Move(direction * currentSpeed * Time.deltaTime);
        anim.SetBool("isWalking", isWalking);
        anim.SetBool("isRunning", isRunning);
    }

    void ApplyRotation()
    {
        if (movement2D.sqrMagnitude == 0) return;

        float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
        float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref currentSpeedRotating, smoothTime);
        transform.rotation = Quaternion.Euler(0.0f, angle, 0.0f);   
    }

    void ApplyGravity()
    {
        if (IsGrounded() && verticalVelocity <0.0f)
        {
            verticalVelocity = -1f;
        }
        else
        {
            verticalVelocity += gravity * gravityMultiplier * Time.deltaTime;
        }

        direction.y =  verticalVelocity;
    }
}

My player has charactercontroller component, capsule collider, Player Input System attached to it and the methods properly linked to the player

答案1

得分: 0

"After testing it and modifying my code, I've figured there was no issue with it, it was actually my keyboard, for some reason it's not working all the keys in the situation I mentioned above, so after I changed my keyboard my code worked."

英文:

After testing it and modying my code, I've figured there was no issue with it, it was actually my keyboard, for some reason it's not working all the keys in the situation I mentioned above, so after I changed my keyboard my code worked

huangapple
  • 本文由 发表于 2023年5月21日 07:42:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定