【发布时间】: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 | 的编码经验。你应该工作。合乎逻辑的或对他们有用...