2017-02-16 60 views
0

與觸摸對象和播放動畫的部分工作正常,現在我想添加牆壁腳本部分。如何在unity3d中一次提升牆並控制提升速度?

在這種情況下,我改變一個立方體的高度。 我需要做的是隻有當玩家觸摸一個物體時,它纔會提升/改變另一個物體的高度。

兒子在當玩家被觸摸對象我覺得第一種情況:

using UnityEngine; 
using System.Collections; 
using System.Reflection; 

public class DetectPlayer : MonoBehaviour { 

    GameObject target; 
    int counter = 0; 

    void OnCollisionEnter(Collision collision) 
    { 
     if (collision.gameObject.name == "ThirdPersonController") // "Platform" 
     { 
      Debug.Log("Touching Platform"); 
     }   
    } 

    void OnTriggerEnter(Collider other) 
    { 
     if (other.gameObject.name == "ThirdPersonController") // "OnTop Detector" 
     { 
      counter = 0; 
      Debug.Log("On Top of Platform"); 
      target = GameObject.Find("Elevator"); 
      GameObject findGo = GameObject.Find("ThirdPersonController"); 
      GameObject findGo1 = GameObject.Find("Elevator"); 
      findGo.transform.parent = findGo1.transform; 

      GameObject go = GameObject.Find("CubeToRaise"); 
      go.GetComponent<RaiseWalls>(); 
      Debug.Log("The button clicked, raising the wall"); 

      StartCoroutine(playAnim(target)); 
     } 
    } 

    void OnTriggerExit(Collider other) 
    { 
     GameObject findGo = GameObject.Find("ThirdPersonController"); 
     findGo.transform.parent = null; 
    } 

    IEnumerator playAnim(GameObject target) 
    { 
     Animation anim = target.GetComponent<Animation>(); 

     foreach (AnimationState clip in anim) 
     { 
      // do initialisation or something on clip 
      clip.speed = 1; 
     } 

     while (true) 
     { 
      if (counter == 1) 
       break; 
      anim.Play("Up"); 

      while (anim.IsPlaying("Up")) 
      { 
       yield return null; 
      } 

      anim.Play("Down"); 

      while (anim.IsPlaying("Down")) 
      { 
       yield return null; 
      } 

      yield return null; 

      counter++; 
     } 
    } 

    void OnGUI() 
    { 
     GUI.Box(new Rect(300, 300, 200, 20), 
      "Times lift moved up and down " + counter); 
    } 
} 

在這部分我打電話,第二腳本RaiseWalls:

using UnityEngine; 
using System.Collections; 

public class RaiseWalls : MonoBehaviour 
{ 

    public GameObject gameObjectToRaise; 
    public float speed; 

    // Use this for initialization 
    void Start() 
    { 
     speed = 2; 
    } 

    void Update() 
    { 

     gameObjectToRaise.transform.localScale += new Vector3(0, 50, 0); 
    } 


} 


GameObject go = GameObject.Find("CubeToRaise"); 
go.GetComponent<RaiseWalls>(); 
Debug.Log("The button clicked, raising the wall"); 

現在DetectPlayer連接在一個遊戲對象上。 RaiseWalls腳本附加在另一個遊戲對象上。

在RaiseWalls劇本我想設置的物體高度變化的速度。現在它的高度改變了50倍,但是很多次。我想讓它改變50點,但是像緩慢的建築/升牆那樣緩慢地移動。就像從底部到頂部效應提升的電子圍欄一樣。

問題二我想,首先它會提高一個或多個壁,一旦它完成了升起的牆壁移動到DetectPlayer腳本的下一部分:

StartCoroutine(playAnim(target)); 

步驟:

  1. 當玩家正在觸摸DetectPlayer腳本中的對象以特定的速度在RaiseWalls腳本中提升牆壁。

  2. 當提出才使StartCoroutine牆壁。

回答

2

所以,你想總共提高牆50,但速度可控。

首先使RaiseWalls計數器:

float raiseAmount; 

而且記錄總培養它,這使得它更容易在以後更改:

float raiseTotal = 50; 

然後,在你更新,以提高它一點點,但記錄了多少提出

if(raiseAmount < raiseTotal) // i.e. we haven't raised it fully 
{ 
    // work out how much to raise it 
    float raiseThisFrame = speed * Time.DeltaTime; // to account for frame rate 
    // now we cap it to make sure it doesn't go over 50 
    if(raiseAmount + raiseThisFrame > raiseTotal) 
    { 
     raiseThisFrame = raiseTotal - raiseAmount; 
    } 

    // add raiseThisFrame to raiseAmount 
    raiseAmount += raiseThisFrame; 

    gameObjectToRaise.transform.localScale += new Vector3(0, raiseThisFrame, 0); 
}