【问题标题】:My player does not always jump when i press the "jump" button当我按下“跳跃”按钮时,我的播放器并不总是跳跃
【发布时间】:2016-10-14 18:50:15
【问题描述】:

在我的 2D Unity 项目中,我的玩家并不总是在按下“跳转”按钮时跳转。我落地的那一秒他没有跳,但是在“接地”一秒后他可以再次跳。这个问题可能是什么?

using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    public class player : MonoBehaviour {

        private static player instance;

        public static player Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = GameObject.FindObjectOfType<player>();
                }
                return instance;
            }

        }


        private Animator myAnimator;

        [SerializeField]
        public static float movementSpeed;

        private bool facingRight = true;

        [SerializeField]
        private Transform[] groundPoints;

        [SerializeField]
        private float groundRadius;

        [SerializeField]
        private LayerMask whatIsGround;

        [SerializeField]
        private bool airControl;

        [SerializeField]
        private float jumpForce;

        public bool canMove;

        public AudioClip jump001;
        public AudioClip jump002;

        private float direction;
        private bool move;
        private float btnHorizontal;

        public Rigidbody2D MyRigidbody { get; set; }

        public bool Attack { get; set; }

        public bool Jump { get; set; }

        public bool OnGround { get; set; }





        // Use this for initialization
        void Start() {

            facingRight = true;
            MyRigidbody = GetComponent<Rigidbody2D>();
            myAnimator = GetComponent<Animator>();




        }
       void Update()
       {

            HandleInput();

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

            OnGround = IsGrounded();

            float horizontal = Input.GetAxis("Horizontal");

            if (move)
            {
                this.btnHorizontal = Mathf.Lerp(btnHorizontal, direction, Time.deltaTime * 5);
                HandleMovement(btnHorizontal);
                Flip(direction);
            }
            else
            {

                HandleMovement(horizontal);

                Flip(horizontal);
            }

            if (!canMove)
            {
                GetComponent<Rigidbody2D>().velocity = new Vector2(0, GetComponent<Rigidbody2D>().velocity.y);
                myAnimator.SetFloat("speed", 0);
                return;
            }




            HandleLayers();
        }


        private void HandleMovement(float horizontal)
        {
            if (MyRigidbody.velocity.y < 0)
            {
                myAnimator.SetBool("land", true);
            }
            if (!Attack && (OnGround || airControl))
            {
                MyRigidbody.velocity = new Vector2(horizontal * movementSpeed, MyRigidbody.velocity.y);
            }
            if (Jump && MyRigidbody.velocity.y == 0)
            {
                SoundManager.instance.RandomizeSfx(jump001, jump002);
                MyRigidbody.AddForce(new Vector2(0, jumpForce));
            }

            myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
        }


        private void HandleInput()
        {
            if (canMove)
            {

                //Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W) ||
                if (Input.GetButtonDown("Jump"))
                {
                    myAnimator.SetTrigger("jump");

                }
                if (Input.GetKeyDown(KeyCode.Z) || Input.GetButton("Fight") && OnGround && !Jump)
                {
                    myAnimator.SetTrigger("attack");
                }
            }
        }

        private void Flip(float horizontal)
        {
            if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight && canMove)
            {
                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)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        private void HandleLayers()
        {
            if (!OnGround)
            {
                myAnimator.SetLayerWeight(1, 1);
            }
            else
            {

                myAnimator.SetLayerWeight(1, 0);
            }

        }

        //TouchKnappar

        public void BtnJump()
        {
            if (canMove)
            {


                myAnimator.SetTrigger("jump");
                Jump = true;
            }
        }
        public void BtnAttack()
        {

                myAnimator.SetTrigger("attack");
                Attack = true;

        }
        public void BtnMove(float direction)
        {

                this.direction = direction;
                this.move = true;

        }
        public void BtnStopMove()
        {

                this.direction = 0;
                this.btnHorizontal = 0;
                this.move = false;

        }
        public void BtnStopJump()
        {

                Jump = false;


    }


    }

【问题讨论】:

  • 你的角色落地时是否有短暂的弹跳,导致检查MyRigidbody.velocity.y &lt;= 0失败?尝试使用Debug.Log() 来验证这一点。
  • 我猜velocity.y在地面上时有点偏离0,需要几秒钟才能完全变为0。velocity.y甚至可能是负数.所以也许将 (Jump && MyRigidbody.velocity.y == 0) 更改为 (Jump && MyRigidbody.velocity.y >= 0) 可能会起作用。但是,老实说,整个代码都应该重新编写......
  • 好的,谢谢,我会调查的。这是我的第一场比赛,我在 youtube 上关注了一个人,并自己做了一些改变。我不完全理解整个代码。
  • @Serlite,是的,我也在想同样的事情。 Marcus Lönnqvist 尝试使用断点检查它何时真正达到这些特定条件。
  • 我可以看到角色有点弹跳,这可能会导致问题。有什么办法可以解决吗?

标签: c# unity3d unity5 unity3d-2dtools


【解决方案1】:

我遇到了同样的问题,这为我解决了问题。

此代码识别字符是否基于 FixedUpdate()

Collider2D groundCol = Physics2D.OverlapBox(groundCheck.position, groundBoxRadius, 0f, whatIsGround);
this.grounded = (groundCol != null && !groundCol.isTrigger && this.rigbody2d.velocity.y > -0.01f && this.rigbody2d.velocity.y < 0.01f);

一些解释:

  • groundCheck 是角色脚下的一个空物体
  • 在这种情况下,我正在检查 Overlay 框是否与某物发生碰撞,以及该物是否不是触发器
  • 最后,速度速度有时可能不完全是 0,所以 +-0.01f 对我有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多