我的Unity角色动画无法正常工作

时间:2019-02-06 01:51:05

标签: c# unity3d

我的角色精灵在动画制作器中有两层。 AirLayer(具有起飞和降落动画)和GroundLayer(BaseLayer)(包含空转和行走动画)。由于某种原因,我的角色卡在了AirLayer上,并且我发现isGrounded布尔值设置为false,但是我不知道如何修复代码。

我已经使用Debug.Log来查看代码中的问题,并且我发现isGrounded出于某种原因始终为假。我也尝试过调整序列化的GroundRadius变量,但没有骰子。角色停留在其着陆动画上

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

public class NewPlayerController : MonoBehaviour 
{
    private Rigidbody2D myRigidBody;
    private Animator anim;
    private SpriteRenderer sr;

    private bool facingRight;

    [SerializeField]
    private Transform[] groundPoints;

    [SerializeField]
private float movementSpeed;

    private bool isGrounded;

    private bool jump;

    [SerializeField]
    private float jumpForce;

    [SerializeField]
    private GameObject bullet;

    // Start is called before the first frame update
    void Start()
    {
        myRigidBody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        sr = GetComponent<SpriteRenderer>();


    }
     void Update()
    {
        HandleInput();
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");

        isGrounded = IsGrounded();

        HandleMovement(horizontal);

        Flip(horizontal);

        HandleLayers();

        ResetValues();
    }

    private void HandleInput()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jump = true;

        }
        if (Input.GetKeyDown(KeyCode.V))
        {
            ShootBullet(0);
        }

    }

    private void HandleMovement(float horizontal)
    {
        if (myRigidBody.velocity.y < 0)
        {
            anim.SetBool("Land", true);

        }

        myRigidBody.velocity = new Vector2(horizontal * movementSpeed, myRigidBody.velocity.y);
        anim.SetFloat("speed", Mathf.Abs(horizontal));

        if(isGrounded && jump)
        {

            isGrounded = false;

            myRigidBody.AddForce(new Vector2(0, jumpForce));

            anim.SetTrigger("Jump"); 

        }

    }
    private void Flip(float horizontal)
    {
        if(horizontal > 0 && !facingRight || horizontal <0 && facingRight)
        {
            facingRight = !facingRight;
            Vector3 theScale = transform.localScale;
            theScale.x *= -1;
            transform.localScale = theScale;
        }
    }
    private bool IsGrounded()
    {
        if (myRigidBody.velocity.y <= 0)
        {
            foreach (Transform point in groundPoints)
            {
                Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
                for (int i = 0; i < colliders.Length; i++)
                {
                    if (colliders[i].gameObject != gameObject)
                    {
                        anim.ResetTrigger("Jump");
                        anim.SetBool("Land", false);
                        return true;

                    }
                }
            }
        }
        return false;

    } 

    private void ResetValues()
    {
        jump = false;
    }
    public void ShootBullet(int value)
    {
        if (facingRight)
        {
            GameObject tmp = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,-90)));
            tmp.GetComponent<BulletBehaviour>().Initialize(Vector2.right);
        }
        else
        {
            GameObject tmp = (GameObject)Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0, 0, 90)));
            tmp.GetComponent<BulletBehaviour>().Initialize(Vector2.left);
        }
    }
    private void HandleLayers()
    {
        if (!isGrounded)
        {
            anim.SetLayerWeight(1, 1);
        }
        else
        {
            anim.SetLayerWeight(1, 0);

        }
    }
}

当isGrounded为true时,该玩家应该走路,但由于某些原因isGrounded不为true,因此卡在其起飞动画上。

0 个答案:

没有答案