由于某些原因,我的角色总是停飞

时间:2019-02-08 00:35:25

标签: c# unity3d

我希望角色仅在接地时才执行跳跃动画。由于某种原因,isGrounded返回false。我该怎么做才能使其返回true?

我使用Debug.Log来发现我的代码返回的IsGrounded为false,但是当我的玩家的Circle collider撞到地面时,我不知道该怎么做。

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 groundRadius;

    [SerializeField]
    private LayerMask whatIsGround;

    [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();
        Debug.Log(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。代码由于某种原因返回false。

1 个答案:

答案 0 :(得分:0)

真的是愚蠢的错误。我只需要稍微调整一下地面和撞机盒。我还必须将地面的图层更改为正确的图层,以便“ whatIsGround”可以让我的Circle对撞机检测到地面