2014-11-03 51 views
-1

您好,感謝您閱讀本文。翻轉圖像/動畫,使其跟隨移動方向

我在我的遊戲中爲我的「怪物」製作了一個小小的動作腳本。

using UnityEngine; 
using System.Collections; 

public class MonsterMovement : MonoBehaviour { 
public Vector3 pointB; 
bool facingLeft = true; 

IEnumerator Start() 
{ 
    var pointA = transform.position; 
    while (true) 
    { 
     yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f)); 
     yield return StartCoroutine(MoveObject(transform, pointB, pointA, 3.0f)); 
    } 
} 

IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time) 
{ 
    var i = 0.0f; 
    var rate = 1.0f/time; 
    while (i < 1.0f) 
    { 
     i += Time.deltaTime * rate; 
     thisTransform.position = Vector3.Lerp(startPos, endPos, i); 
     yield return null; 
    } 
} 

這個腳本很簡單,它把對象/怪物從 - > b移回來。並不斷重複它。

但是,我怎樣才能設法翻轉物體的圖像,以便它遵循運動的方向。

我真的希望你能幫助我。

非常感謝。

回答

0

我找到了解決我的問題的方法。

我加入這個小碼:

bool facingLeft = true; 

void Flip(){ 
    facingLeft = !facingLeft; 
    Vector3 theScale = transform.localScale; 
    theScale.x *= -1; 
    transform.localScale = theScale; 
} 

,然後改變其功能時:

while (true) 
    { 
     yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f)); 
     if (!facingLeft) { 
      Flip(); 
     } 
     yield return StartCoroutine(MoveObject(transform, pointB, pointA, 3.0f)); 
     if (facingLeft) { 
      Flip(); 
     } 
    }