布尔没有变错假团结

时间:2013-12-25 15:40:46

标签: c# boolean unity3d

我正在努力学习PushyPixels我在第2集中的统一2d系统,并且我试图在不接触地面时将布尔设置为假。它不会设置为false,除非我使用grounded = false;。我正在尝试一切。我知道游戏对象在阵列中,我知道当我从平台上掉下来时它们并没有触地。怎么了?

using UnityEngine;
using System.Collections;

public class Jump : MonoBehaviour {

public string jumpButton = "Fire1";
public float jumpPower = 10.0f;
public Animator anim;
public float minJumpDelay = 0.05f;
public Transform[] groundChecks;
public float jumpTime = 0.0f;
public bool grounded;
// Use this for initialization
void Start () 
{
    anim = gameObject.GetComponent<Animator>();
    grounded = true;
}

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

    foreach(Transform groundCheck in groundChecks)
    {
        grounded = grounded | Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
    }
    anim.SetBool("Grounded", grounded);
    if(jumpTime > 0) {
        jumpTime -= Time.deltaTime;
    }
    if(Input.GetButtonDown(jumpButton) && anim.GetBool("Grounded")) 
    {
        anim.SetBool("Jump", true);
        grounded = false;
        rigidbody2D.AddForce(transform.up * jumpPower);
        jumpTime = minJumpDelay;
    }
    if(anim.GetBool("Grounded") && jumpTime <= 0)
    {
        anim.SetBool("Jump", false);
    }
}

}

2 个答案:

答案 0 :(得分:1)

在foreach循环之前的Update例程中,您必须设置grounded = false。这样你就可以假设你在空中,除非至少有一个地面检查告诉你。

答案 1 :(得分:0)

你可能已经修好了,但如果没有,请使用:

   public float GroundRadius = 0.1f;//change as needed until done testing then remove public
   public LayerMask WahtIsGround;//you can set the ground layermask from the inspector this way or make it private and set it on awake.

公共变换GroundCheck;我也会把它设置为清醒,但是你可以把它放进去。

另外,在FixedUpdate()

中放入物理内容
   void FixedUpdate()
   {
          Grounded = Physics2D.OverlapCircle (GroundCheck.position, GroundRadius, WhatIsGround1);
   }
相关问题