【问题标题】:Bool not turning false Unity布尔不会变成假 Unity
【发布时间】:2014-01-13 11:49:47
【问题描述】:

我正在尝试从 PushyPixels 学习统一 2d 系统我在第 2 集,并且我正在尝试在不接触地面时将 bool 设置为 false。它不会将自己设置为 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);
    }
}

}

【问题讨论】:

  • 你有grounded = grounded | ...。这不应该是合乎逻辑的还是||?请记住,对于逻辑或,当接地以true 开头时,它永远不会在该行变为false。但这只是一个备注。我不确定这是你的问题。
  • 感谢您的回复。我尝试了逻辑或,但没有奏效。这就是 Pushy Pixels 使用的,但根据我对 x= x | 的编码经验。你应该工作。合乎逻辑的或对他们有用...

标签: c# boolean unity3d


【解决方案1】:

您可能已经解决了这个问题,但如果没有,请使用:

   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;我也会将它设置为 awake,但在这里你可以把它放进去。

另外,把物理的东西放在 FixedUpdate() 中

   void FixedUpdate()
   {
          Grounded = Physics2D.OverlapCircle (GroundCheck.position, GroundRadius, WhatIsGround1);
   }

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-02-08
      • 2020-01-21
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多