2016-02-13 59 views
0

我是C#和Unity3D的初學者,所以我的問題可能很奇怪,但請不要評價我。我很難弄清楚什麼是錯的,爲什麼它不起作用。輸入衝突後將gameObject移動到另一個對象。 Unity C#

我在遊戲中有鬼,當我靠近它時,它必須離開我。我已經創建了周圍的幽靈碰撞並添加這個腳本到它:

​​

無論如何,當我在碰撞框移動,一切正常(它會破壞遊戲的對象「唱」和集光「假」),但它從不會將gameObject的「鬼影」位置移動到另一個對象「目標」。我的速度設置爲5,並且分配了所有對象。

+0

你確定幽靈和目標有不同的位置,他們不是父母的孩子嗎? –

+0

@Hanza Hasan我相信。 – NeuTronas

回答

1

那麼,你只執行Vector3.MoveTowards只有一次,所以你的ghost只移動一步。你需要的是在Update與任何標誌的幫助,在Coroutine下一些condition.Like執行此,

using UnityEngine; 
using System.Collections; 

public class MaidTriggeris : MonoBehaviour { 
    public GameObject light; 
    public GameObject sing; 
    public GameObject ghost; 
    public float speed; 
    public GameObject target; 


    // Use this for initialization 
    void Start() { 
     light.SetActive(true); 
    } 

    // Update is called once per frame 
    void OnTriggerEnter(){ 
     light.SetActive (false); 
     DestroyObject (sing); 
     StartCoroutine("MoveGhost");    
    } 

    IEnumerator MoveGhost(){ 
     while(Vector3.Distance(ghost.transform.position, target.transform.position) > 1.0f) // Change this value accordingly 
      { 
       float step = speed * Time.deltaTime; 
       ghost.transform.position = Vector3.MoveTowards(ghost.transform.position, target.transform.position, step); 
       yield return new WaitForEndOfFrame(); 
      } 
    } 
} 

上面的代碼片段並不雖然測試。所以如果需要做一些調整。

相關問題