我有一個小腳本導致對象來回跳動。這是一個向上滾動無盡跑步者的鳥。所以它代表了它的飛行路線。這個腳本將它從一端移動到另一端,當它到達結尾時,它翻轉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