【问题标题】:I'm trying to make my 2D unity character jump我正在尝试让我的 2D 统一角色跳跃
【发布时间】:2020-05-25 05:47:04
【问题描述】:

正如我所说,我试图让我的角色在 Unity 中跳跃,但是当我点击空间时什么都没有发生,并且 Unity 没有抛出任何错误。

public class Player : MonoBehaviour
{
    private Rigidbody2D myRigidbody;

    [SerializeField]
    public float jumpSpeed;

    private Animator myAnimator;

    [SerializeField]
    private float movementSpeed;

    private bool facingLeft;

    [SerializeField]
    private Transform[] groundPoints;

    [SerializeField]
    private float groundRadius;

    [SerializeField]
    private LayerMask whatIsGround;

    private bool isGrounded;

    private bool jump;

    [SerializeField]
    private float jumpForce;

    // Start is called before the first frame update
    void Start()
    {
        facingLeft = true;
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");
        Debug.Log(horizontal);

        isGrounded = IsGrounded();

        HandleMovement(horizontal);

        flip(horizontal);

    }

    private void HandleMovement(float horizontal)
    {

        myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);

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

        if (isGrounded && jump)
        {
            isGrounded = false;
            myRigidbody. AddForce(new Vector2(0, jumpForce));
        }

    }

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

    private void flip(float horizontal)
    {
        if (horizontal < 0 && !facingLeft || horizontal > 0 && facingLeft)
        {
            facingLeft = !facingLeft;

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

}

我一直在关注this 的教程,他在跳,但我的不是。我认为这可能与我设置接地点的方式有关,但我不确定这是代码中的错误还是 Unity 中的错误。

【问题讨论】:

  • 由于某种原因,第一行代码没有出现在代码框中,它应该在里面。
  • 脚本是否附加在游戏对象上,值是否填写在序列化字段中?为什么是固定更新而不是定期更新??
  • 为什么你认为错误来自groundPoints设置?你是如何配置它们的?
  • "为什么是固定更新而不是定期更新??"这样游戏的速度就不会因你的 fps 而改变。
  • "为什么你认为错误来自groundPoints设置?你是如何配置它们的?"主要是因为这是我第一次这样做,我想我可能搞砸了,我在角色上有三个接地点,一个在左下角,一个在右下角,一个在中间这两个。

标签: c# unity3d


【解决方案1】:

你没有在任何地方调用 HandleInput。

添加到固定更新

void FixedUpdate()
{
    float horizontal = Input.GetAxis("Horizontal");
    Debug.Log(horizontal);

isGrounded = IsGrounded();

HandleMovement(horizontal);

flip(horizontal);

HandleInput(); // add this so you are sampling input

}

【讨论】:

  • 抱歉回复晚了,我在代码中添加了这个,没有任何改变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多