2017-02-16 5 views
0

オブジェクトに触れてアニメーションを再生している部分がうまくいきました。壁のスクリプト部分を追加したいと思います。unity3dで壁を一度上げて持ち上げ速度をコントロールするにはどうすればいいですか?

この場合、私はキューブの高さを変更します。 私は、プレイヤーがオブジェクトに触れるときにのみ、別のオブジェクトの高さを上げたり変更したりする必要があります。今DetectPlayerが装着されている

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"); 

:私は2番目のスクリプトRaiseWallsを呼んでいる。この部分では

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); 
    } 
} 

:プレイヤーがオブジェクトに触れているときに私が見つける最初のケースで

息子1つのゲームオブジェクトに RaiseWallsスクリプトは別のゲームオブジェクトに添付されています。

RaiseWallsスクリプトでは、オブジェクトの高さの変更の速度を設定します。今は高さを50倍に変更しています。私はそれを50だけ変化させたいが、ゆっくりとした建物/壁を上げるようなスローモーションでしたい。下から上へと上がる電子フェンスのように。

私はそれは壁がDetectPlayerスクリプトの次の部分に移動上げ終わったら、まずそれが壁や壁を調達することを望ん

第二の問題:

StartCoroutine(playAnim(target)); 

ステップ:

  1. プレーヤーはDetectPlayerスクリプトでオブジェクトに触れているRaiseWallsスクリプトのwall/sを特定の速度で上げます。

  2. 壁が隆起したときだけ、StartCoroutineを作成します。

答えて

2

したがって、制御可能な速度で、合計で壁50を上げたいとします。

まずRaiseWallsにカウンターを作る:ことによってそれを上げる、あなたのアップデートでは、次に

float raiseTotal = 50; 

float raiseAmount; 

はまた、それが簡単に後から変更することができ、それを高めるために、合計を記録ちょっと、しかし、どのくらい育ったか記録する。

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); 
} 
関連する問題