简体   繁体   English

Unity 2D 为什么我的角色在地面不平坦时不会跳跃?

[英]Unity 2D Why doesn't my character jump when the ground isn't flat?

My character is bouncing on a flat surface, as shown in the picture.我的角色在平面上弹跳,如图所示。 but it doesn't jump when the floor is crooked.I don't know what I'm doing wrong.但是当地板弯曲时它不会跳。我不知道我做错了什么。 my character is jumping on flat ground.我的角色在平地上跳跃。 but my character doesn't jump when the ground isn't flat.但是当地面不平坦时,我的角色不会跳跃。

在此处输入图片说明

My Jump Script here:我的跳转脚本在这里:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class WalkJumpFire : MonoBehaviour {

    public CharacterController2D kontroller;
    Rigidbody2D rb;
    float dirX;

    [SerializeField]
    float moveSpeed = 5f, jumpForce = 800f, bulletSpeed = 500f;

    bool facingRight = true;
    Vector3 localScale;

    public Transform barrel;
    public Rigidbody2D bullet;

    // Use this for initialization
    void Start () {
        localScale = transform.localScale;
        rb = GetComponent<Rigidbody2D> ();
    }

    // Update is called once per frame
    void Update () {
        dirX = CrossPlatformInputManager.GetAxis ("Horizontal");

        if (CrossPlatformInputManager.GetButtonDown ("Jump"))
            Jump ();

        if (CrossPlatformInputManager.GetButtonDown ("Fire1"))
            Fire ();
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector2 (dirX * moveSpeed, rb.velocity.y);

    }

    void LateUpdate()
    {
        CheckWhereToFace ();
    }

    void CheckWhereToFace()
    {
        if (dirX > 0)
            facingRight = true;
        else
            if (dirX < 0)
                facingRight = false;
        if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
            localScale.x *= -1;
        transform.localScale = localScale;
    }

    void Jump()
    {
        if (rb.velocity.y == 0)
            rb.AddForce (Vector2.up * jumpForce);
    }

    void Fire()
    {
        var firedBullet = Instantiate (bullet, barrel.position, barrel.rotation);
        firedBullet.AddForce (barrel.up * bulletSpeed);
    }
}

As @derHugo mentioned, use the condition of your rigidbody lying on the ground (via isGrounded method) to instigate your jump:正如@derHugo 提到的,使用刚体躺在地上的条件(通过 isGrounded 方法)来激发你的跳跃:

public bool isGrounded;  

//During collisions you would still want your object to jump, such as jumping while touching the corner of a wall, so:
void OnCollisionStay()
{
        isGrounded = true;
}  

//Your not able to jump right after collision: (Also add layers so your object can't pass through walls or entities in the game)
void OnCollisionExit()
{
        isGrounded = false;
}

//Jump only if your object is on ground:
void Jump()
{
       if(isGrounded)
       {
          rb.AddForce (Vector2.up * jumpForce);
          //set it to false again so you can't multi jump:
          isGrounded = false;
       }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM