我有一个脚本,移动字符的方向取决于它与什么碰撞。下面是我的代码:
var speed : float = 1;
private var leftBool : boolean;
private var rightBool : boolean;
private var upBool : boolean;
private var downBool : boolean;
function Update () {
if(leftBool == true) {
gameObject.GetComponent(Rigidbody2D).velocity.x = gameObject.GetComponent(Rigidbody2D).velocity.x - speed * Time.deltaTime;
rightBool = false;
upBool = false;
downBool = false;
}
if(upBool == true) {
gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y + speed * Time.deltaTime;
rightBool = false;
leftBool = false;
downBool = false;
}
if(rightBool == true) {
gameObject.GetComponent(Rigidbody2D).velocity.x = gameObject.GetComponent(Rigidbody2D).velocity.x + speed * Time.deltaTime;
rightBool = false;
leftBool = false;
downBool = false;
}
if(downBool == true) {
gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y - speed * Time.deltaTime;
rightBool = false;
leftBool = false;
upBool = false;
}
}
function Start () {
leftBool = true;
}
function OnTriggerEnter2D (other : Collider2D) {
if(other.GetComponent(Arrows).rotationNumber == 1) {
leftBool = true;
Debug.Log("Entered");
}
if(other.GetComponent(Arrows).rotationNumber == 2) {
upBool = true;
Debug.Log("Entered");
}
if(other.GetComponent(Arrows).rotationNumber == 3) {
rightBool = true;
Debug.Log("Entered");
}
if(other.GetComponent(Arrows).rotationNumber == 4) {
downBool = true;
Debug.Log("Entered");
}
}这一切为什么要发生?谢谢。
发布于 2015-03-01 15:21:28
起初,当假设与rotationNumber==1发生冲突时,leftBool仍然为真。根据声明和Update()中的代码,其他代码为false。现在,当您与rotationNumber==2发生冲突时,upBool将返回true。但如前所述,在Update()中,leftBool仍然为true,并且它位于代码执行顺序的顶部,upBool将立即返回false。所以,你永远不会得到if(upBool == true){}个东西(顺便说一句,你不必使用==操作符来表示布尔,if(upBool)表示true,if(!upBool)表示false)。希望它能澄清你的困惑。现在,有很多方法可以实现你想要的。但是,我修改了你的代码,这样你就能明白发生了什么-
var speed : float = 1;
private var leftBool : boolean;
private var rightBool : boolean;
private var upBool : boolean;
private var downBool : boolean;
function Start () {
leftBool = true;
}
function Update () {
if(leftBool) {
gameObject.GetComponent(Rigidbody2D).velocity.x = gameObject.GetComponent(Rigidbody2D).velocity.x - speed * Time.deltaTime;
}
if(upBool) {
gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y + speed * Time.deltaTime;
}
if(rightBool) {
gameObject.GetComponent(Rigidbody2D).velocity.x = gameObject.GetComponent(Rigidbody2D).velocity.x + speed * Time.deltaTime;
}
if(downBool) {
gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y - speed * Time.deltaTime;
}
}
function OnTriggerEnter2D (other : Collider2D) {
if(other.GetComponent(Arrows).rotationNumber == 1) {
leftBool = true;
rightBool = false;
upBool = false;
downBool = false;
Debug.Log("Entered");
}
if(other.GetComponent(Arrows).rotationNumber == 2) {
upBool = true;
rightBool = false;
leftBool = false;
downBool = false;
Debug.Log("Entered");
}
if(other.GetComponent(Arrows).rotationNumber == 3) {
rightBool = true;
upBool = false;
leftBool = false;
downBool = false;
Debug.Log("Entered");
}
if(other.GetComponent(Arrows).rotationNumber == 4) {
downBool = true;
rightBool = false;
leftBool = false;
upBool = false;
Debug.Log("Entered");
}
}希望这能有所帮助。
https://stackoverflow.com/questions/28789044
复制相似问题