我有一个小脚本导致对象来回跳动。这是一个向上滚动无尽跑步者的鸟。所以它代表了它的飞行路线。这个脚本将它从一端移动到另一端,当它到达结尾时,它翻转2D精灵并以相反的方向传播。它大部分时间都在工作。但问题是,有时图像翻转两次,现在它看起来像飞回来,直到它再次。每次它做它似乎是随机的。对象不正确定位
public class Fly : MonoBehaviour {
private bool dirRight = false;
public float speed;
public bool facingRight = false;
void Start(){
speed = Random.Range (15.0f, 22.0f);
}
void Update() {
if(transform.position.x >= 25.0f) {
dirRight = false;
Flip();
}
if(transform.position.x <= -25.0f) {
dirRight = true;
Flip();
}
if (dirRight)
transform.Translate (Vector2.right * speed * Time.deltaTime);
else
transform.Translate (-Vector2.right * speed * Time.deltaTime);
}
void Flip()
{
// Switch the way the player is labelled as facing
facingRight = !facingRight;
// Multiply the player's x local scale by -1
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
我修改了if语句,用我的bool以及位置这样:
if(transform.position.x >= 25.0f && dirRight == true) {
dirRight = false;
Flip();
}
if(transform.position.x <= -25.0f && dirRight == false) {
dirRight = true;
Flip();
}
我现在运行它,并等着看它是否工作。
每当'Flip'调用时你有没有试过记录'transform.position'? – NeverHopeless