unity3d Unity Animator状态参数未正确更改

omqzjyyz  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(211)

我有一个问题,动画师是改变状态参数为3出于某种原因,每当我移动到右边。我一直在检查我的代码,我不知道为什么会发生这种情况。它以前工作正常,我唯一改变的是,我切换了我原来使用的精灵。
以下是到目前为止我所做的尝试和解决问题的方法:
1.我还确保调整我的碰撞机,以配合新的精灵大小以及。
1.我在我的Animator中有一个名为“State”的参数,并使用debug.log来尝试找出UpdateAnimationState中是否有我的代码部分(),这会导致它出错,但是每次它都显示我正在切换到正确的状态,但是它很快就将状态更新为3(跳跃).这似乎只发生在我向右移动的时候,但有一次它发生在我向左移动的时候.我不知道这是怎么变成向右移动的,但现在它只在那个方向上做.
1.我尝试重新制作整个对象,以防在交换精灵时出现问题,但它仍然发生在新对象上。
任何帮助将不胜感激,如果有人是免费的星期六,我可以通过discord分享我的屏幕,以显示实际的项目和任何设置,我有设置,层次结构,动画设置等,或者如果它更容易,我可以通过github分享项目文件。
下面是我的代码:

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

// "RequireComponent" ensures that the rigidbody component exists on the current gameObject and the Rigidbody cannot be removed before the PlayerMovement script is removed.
[RequireComponent(typeof(Rigidbody2D))]

public class PlayerMovement : MonoBehaviour
{
    #region Primary Variables
    private Rigidbody2D rb;
    private Animator anim;
    public Transform groundCheck;
    public LayerMask groundLayer;
    public AudioSource jumpSFX;
    

    [SerializeField] private float moveSpeed = 7.1f;
    [SerializeField] private float jumpStrength = 11f;
    [SerializeField] private float fallSpeed = 4f;

    private float raycastDistance = 0.2f;
    private float startingSpeed = 0f;
    private float horizontal;
    private bool facingRight = true;
    private bool doubleJump;
    private float coyoteTime = 0.15f;
    private float coyoteTimeCounter;
    private float airborneSpeedReduction = 1.4f;
    private float acceleration = 19.5f;
    #endregion

    private enum MovementState { idle, running, jumping, falling }

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {

        // Reduces movespeed while in the air
        if (!Grounded())
        {
            jumpingMoveSpeed(); 
        }

        // Applies coyote time
        if (Grounded())
        {
            coyoteTimeCounter = coyoteTime;            
        }
        else
        {
            coyoteTimeCounter -= Time.deltaTime;
        }

        // Flips the character based on the move direction
        if (!facingRight && horizontal > 0f)
        {
            Flip();
        }
        else if (facingRight && horizontal < 0f)
        {
            Flip();
        }

        // Raycast down to check if the player is grounded
        if (Physics2D.Raycast(groundCheck.position, Vector2.down, raycastDistance, groundLayer))
        {
            doubleJump = true;
        }
        SmoothMovement();
        UpdateAnimationState();
    }

    private void FixedUpdate()
    {
        // Allows the character to continue moving forward when jumping
        rb.velocity = new Vector2(horizontal * startingSpeed, rb.velocity.y);

        // Increases gravity when the character reaches the peak of their jump
        if (rb.velocity.y < 0f)
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (fallSpeed - 1f) * Time.deltaTime;
        }

        // This gradually increases moveSpeed when moving instead of starting at the max moveSpeed.

    }

    private void SmoothMovement()
    {
        // Smooths out the movement so that the character doesn't start at max speed.
        // Instead it starts from 0 then goes up to the moveSpeed value.
        if (horizontal == 0)
        {
            startingSpeed = 0;
        }
        if(horizontal != 0 && Grounded())
        {
            if (startingSpeed < moveSpeed)
            {
                startingSpeed += acceleration * Time.deltaTime;
                if (startingSpeed > moveSpeed)
                {
                    startingSpeed = moveSpeed;
                }
            }
        }
    }

    // Movement Methods
    public void Move(InputAction.CallbackContext context)
    {
        horizontal = context.ReadValue<Vector2>().x;
    }

    public void Jump(InputAction.CallbackContext context)
    {
        if (context.performed) // Remove coyoteTimeCounter > 0 if this doesn't work
        {
            if (coyoteTimeCounter > 0) // This used to say Grounded() but has been changed to coyoteTime > 0. Remove this note once testing is done.
            {
                jumpingMoveSpeed();
                rb.velocity = new Vector2(rb.velocity.x, jumpStrength);
                doubleJump = true;
                jumpSFX.Play();
            }
            else if (doubleJump)
            {
                jumpingMoveSpeed();
                rb.velocity = new Vector2(rb.velocity.x, jumpStrength);
                doubleJump = false;
                coyoteTimeCounter = 0f;
                jumpSFX.Play();
            }
        }
        else if (context.canceled)
        {
            if (rb.velocity.y > 0)
            {
                rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
            }
        }
    }

    private void Flip()
    {
        facingRight = !facingRight;
        Vector3 localeScale = transform.localScale;
        localeScale.x *= -1f;
        transform.localScale = localeScale;
    }

    private bool Grounded()
    {
        // Check if the player is grounded using raycasting
        RaycastHit2D hit = Physics2D.Raycast(groundCheck.position, Vector2.down, raycastDistance, groundLayer);
        return hit.collider != null;
    }

    private void jumpingMoveSpeed()
    {
        startingSpeed = moveSpeed;
        startingSpeed = startingSpeed / airborneSpeedReduction;
    }

    private void UpdateAnimationState()
    {
        MovementState state;

        if (horizontal > 0 || horizontal < 0)
        {
            state = MovementState.running;
        }
        else
        {
            state = MovementState.idle;
        }

        if (rb.velocity.y > 0f)
        {
            state = MovementState.jumping;
        }
        else if (rb.velocity.y < 0f)
        {
            state = MovementState.falling;
        }

        anim.SetInteger("State", (int)state);
    }
}
wecizke3

wecizke31#

我能够通过将秋季检查的值更改为负值来解决这个问题。

private void UpdateAnimationState()
    {
        MovementState state;

        if (horizontal > 0 || horizontal < 0)
        {
            state = MovementState.running;
        }
        else
        {
            state = MovementState.idle;
        }

        if (rb.velocity.y > 0.1f)
        {
            state = MovementState.jumping;
        }
        else if (rb.velocity.y < -0.1f)
        {
            state = MovementState.falling;
        }

        anim.SetInteger("State", (int)state);
    }
}

相关问题