2016-09-11 47 views
-1

我試圖通過觸發器激活腳本。我有一個使對象浮動並旋轉的腳本,但我希望在玩家靠近對象時啓動該腳本。此時,遊戲開始時浮動對象,而不是玩家激活觸發器時。如何通過統一觸發器啓動腳本

這裏是浮動的腳本:

public class AutoMoveAndRotate : MonoBehaviour 
{ 
    public Vector3andSpace moveUnitsPerSecond; 
    public Vector3andSpace rotateDegreesPerSecond; 
    public bool ignoreTimescale; 
    private float m_LastRealTime; 


    private void Start() 
    { 
     m_LastRealTime = Time.realtimeSinceStartup; 
    } 


    // Update is called once per frame 
    private void Update() 
    { 
     float deltaTime = Time.deltaTime; 
     if (ignoreTimescale) 
     { 
      deltaTime = (Time.realtimeSinceStartup - m_LastRealTime); 
      m_LastRealTime = Time.realtimeSinceStartup; 
     } 
     transform.Translate(moveUnitsPerSecond.value*deltaTime, moveUnitsPerSecond.space); 
     transform.Rotate(rotateDegreesPerSecond.value*deltaTime, moveUnitsPerSecond.space); 
    } 


    [Serializable] 
    public class Vector3andSpace 
    { 
     public Vector3 value; 
     public Space space = Space.Self; 
    } 
} 
} 

這裏是觸發腳本(連接到放置浮動對象之前的對象):

public class ActivateTrigger : MonoBehaviour 
{ 
    // A multi-purpose script which causes an action to occur when 
    // a trigger collider is entered. 
    public enum Mode 
    { 
     Trigger = 0, // Just broadcast the action on to the target 
     Replace = 1, // replace target with source 
     Activate = 2, // Activate the target GameObject 
     Enable = 3,  // Enable a component 
     Animate = 4, // Start animation on target 
     Deactivate = 5 // Decativate target GameObject 
    } 

    public Mode action = Mode.Activate;   // The action to accomplish 
    public Object target;      // The game object to affect. If none, the trigger work on this game object 
    public GameObject source; 
    public int triggerCount = 1; 
    public bool repeatTrigger = false; 


    private void DoActivateTrigger() 
    { 
     triggerCount--; 

     if (triggerCount == 0 || repeatTrigger) 
     { 
      Object currentTarget = target ?? gameObject; 
      Behaviour targetBehaviour = currentTarget as Behaviour; 
      GameObject targetGameObject = currentTarget as GameObject; 
      if (targetBehaviour != null) 
      { 
       targetGameObject = targetBehaviour.gameObject; 
      } 

      switch (action) 
      { 
       case Mode.Trigger: 
        if (targetGameObject != null) 
        { 
         targetGameObject.BroadcastMessage("DoActivateTrigger"); 
        } 
        break; 
       case Mode.Replace: 
        if (source != null) 
        { 
         if (targetGameObject != null) 
         { 
          Instantiate(source, targetGameObject.transform.position, 
             targetGameObject.transform.rotation); 
          DestroyObject(targetGameObject); 
         } 
        } 
        break; 
       case Mode.Activate: 
        if (targetGameObject != null) 
        { 
         targetGameObject.SetActive(true); 
        } 
        break; 
       case Mode.Enable: 
        if (targetBehaviour != null) 
        { 
         targetBehaviour.enabled = true; 
        } 
        break; 
       case Mode.Animate: 
        if (targetGameObject != null) 
        { 
         targetGameObject.GetComponent<Animation>().Play(); 
        } 
        break; 
       case Mode.Deactivate: 
        if (targetGameObject != null) 
        { 
         targetGameObject.SetActive(false); 
        } 
        break; 
      } 
     } 
    } 


    private void OnTriggerEnter(Collider other) 
    { 
     DoActivateTrigger(); 
    } 
} 
} 

如果我浮腳本添加到無論玩家是否通過觸發器,它都會開始浮動。所以不知道如何使這項工作?

請幫忙?

非常感謝

回答

0

的浮動對象上的AutoMoveAndRotate組件,你應該在開始遊戲之前停用。否則它會在遊戲開始後執行。

+0

當然!爲什麼我沒有想到這一點! :-) 謝謝! – Newbie

+0

這是一個常見的錯誤。 ;) – zwcloud