我有一个脚本可以在方向上移动角色取决于它碰撞的内容。继承我的代码:
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");
}
}
它显示"输入"在debug.log中但角色在碰撞时没有改变方向。为什么会这样?感谢。
答案 0 :(得分:1)
首先当与rotationNumber == 1发生碰撞时,leftBool保持为true。根据声明和Update()中的代码,其他错误。现在,当你与rotationNumber == 2发生碰撞时,upBool返回true。但是如前所述,leftBool在Update()中仍然是正确的,并且它位于代码执行顺序的顶部,upBool将立即返回false。所以,你永远不会得到if(upBool == true){}的东西(顺便说一下,你不必使用==运算符来表示bools,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");
}
}
希望它会有所帮助。