2016-04-21 35 views
0

我一棟齒輪VR APP,我想我的性格縮減在進入箱撞機區,我可以通過它的規模了所有的突然使用transform.localScale = new Vector3(0.3F,0.3F,0.3F);但我想它做smoothly.Dont KNW爲什麼它不拿起這個線性插值線項目可以在任何一個helpp的原因???我標記我的盒子對撞機(立方體「瑪尼」)還有一兩件事,我OnTriggerExit線性插值一個也沒有打電話。Lerp.localScale不工作OnTriggerEnter,Unity5.3.3,齒輪VR應用開發

#pragma strict 

var newScale : Vector3 = Vector3 (0.1,0.1,0.1); 

var Grow : Vector3 = Vector3 (1,1,1); 
var speed : float =2.0; 

function Start() { 
    transform.localScale = new Vector3(1F,1F,1F); 
} 

function Update() { 

} 

function OnTriggerEnter (info : Collider) { 

    if(info.tag == "Mani") { 
    transform.localScale =Vector3.Lerp(transform.localScale, newScale, speed * Time.deltaTime/2); 
    //transform.localScale = new Vector3(0.3F,0.3F,0.3F); 
    Debug.Log("Player hit new cube"); 
    } 

} 


function OnTriggerExit (Col : Collider) { 

    if(Col.tag == "Mani") { 
    // transform.localScale = new Vector3(transform.localScale.x, 1F, transform.localScale.y); 
    transform.localScale =Vector3.Lerp(newScale, Grow, speed * Time.deltaTime); //transform.localScale = new Vector3(1F,1F,1F); Debug.Log("Player left cube"); 
    } 

} 

回答

0

您誤解了Lerp的功能。 Lerp本身不會動畫任何東西,它只是通過t在兩個值之間進行插值,其中t的範圍是0到1.您可以將t設想爲百分比 - >如果t爲0,則lerp的結果是第一個值(或者在v3的情況下也是在第二個方向上0%),如果t是1,結果是第二個,如果t是0.5,結果位於兩者的中間,所以如果它的0.25在25%(Slerp是基本上是一樣的,只不過這個值代表一個輪換,查看維基百科的更多信息)。

但到底是什麼,到底對你意味着什麼?

你需要調用線性插值多次和t設置爲你在時間,其中。一種選擇是,一旦玩家進入/退出觸發器,開始一個協同程序並逐漸減少每幀(1/seconds) * Time.deltaTime

編輯:

附加到一個可見的gameobject並運行它。它認爲它應該清楚Lerp的工作方式。重要的部分是t。它需要隨着時間的推移而減少以產生動畫效果。

using UnityEngine; 

public class LerpExample : MonoBehaviour { 

    public Vector3 targetScale = new Vector3(2, 2, 2); 
    public float duration = 2; 

    Vector3 originalScale; 
    float timeFactor; 
    float t; 

    void Start() { 
     originalScale = transform.localScale; 

     //since only values between 0 and 1 will do something 
     //we need to know "how much" Time.deltaTime affects our lerping 
     //ofc we could calculate it "on the fly", but it stays the same so better store it 
     timeFactor = 1/duration; 

     t = 0; 
    } 

    void Update() { 

     //this is just so it repeats over and over 
     //if t would grow bigger than 1, it would just be interpreted as 1 by Lerp 
     if (t > 1) t -= 1; 

     //this is what animates it in the end. each frame (its the update method!) a fraction gets added to t. think of it as percentage. 
     t += timeFactor * Time.deltaTime; 
     transform.localScale = Vector3.Lerp(originalScale, targetScale, t); 

    } 
} 

edit2:好的,這是上面運行的協程。雖然這是一個小的「陷阱」。當縮放結束前玩家離開觸發器時,我沒有考慮過。如果他這樣做,則持續時間不一致。如果例如10秒,他在2秒後離開,他將在10秒內縮回到正常,而不是2秒。如果你想修復它,我會讓你解決。

此進至觸發

using UnityEngine; 

public class Cake : MonoBehaviour { 

    public Vector3 targetScale = new Vector3(2, 2, 2); 
    public float duration = 2; 

    void OnTriggerEnter(Collider other) { 
     Alice alice = other.GetComponentInParent<Alice>(); 
     if (alice == null) return; 
     alice.Eat(this); 
    } 
    void OnTriggerExit(Collider other) { 
     Alice alice = other.GetComponentInParent<Alice>(); 
     if (alice == null) return; 
     alice.GrowBackToNormal(this); 
    } 

} 

和這正好到物體進入觸發

using UnityEngine; 
using System.Collections; 

public class Alice : MonoBehaviour { 

    Vector3 originalScale; 
    Coroutine eatAllTheCakeCoroutine; 

    void Start() { 
     originalScale = transform.localScale; 
    } 

    IEnumerator ChangeScale(Vector3 targetScale, float duration) { 
     Vector3 startScale = transform.localScale; 
     float timeFactor = 1/duration; 
     float t = 0; 
     while (t < 1) { 
      t += timeFactor * Time.deltaTime; 
      transform.localScale = Vector3.Lerp(startScale, targetScale, t); 
      yield return null; 
     } 
     transform.localScale = targetScale; 
    } 

    public void Eat(Cake cake) { 
     if (eatAllTheCakeCoroutine != null) StopCoroutine(eatAllTheCakeCoroutine); 
     eatAllTheCakeCoroutine = StartCoroutine(ChangeScale(cake.targetScale, cake.duration)); 

    } 
    public void GrowBackToNormal(Cake cake) { 
     if(eatAllTheCakeCoroutine != null) StopCoroutine(eatAllTheCakeCoroutine); 
     eatAllTheCakeCoroutine = StartCoroutine(ChangeScale(originalScale, cake.duration)); 
    } 
} 
+0

三江源洙多YES,有一個笨錯誤,一行代碼放置在礦井開始函數不會讓lerp函數去完成它的工作。函數Start(){//transform.localScale = new Vector3(1F,1F,1F); //這是愚蠢的行。 // -------------------------- –

+0

現在我的ontriggerEnter和Exit r工作,但是當我激活On觸發器退出代碼行時,我的OVR字符開始在觸發區內晃動// ------函數OnTriggerStay(info:Collider){if(info.tag ==「Mani1」){transform.localScale = Vector3.Lerp(transform.localScale,newScale,speed * Time.deltaTime); //transform.localScale =新的Vector3(0.9F,0.9F,0.9F); Debug.Log(「Player Mani1」); } } // ----------- function OnTriggerExit(info:Collider){if(info.tag ==「Mani1」){transform.localScale = Vector3.Lerp(transform。 localScale,Grow,speed * Time.deltaTime); } } –

+0

你需要增加T(線性插值的第三個參數)。當你縮放時,它應該從0到1。在第一幀t應該是0,在下一個你通過'(1 /秒)* TimeDeltaTime'增加它,並且你繼續增加每個後續幀,直到你達到1爲止。http://docs.unity3d.com/ScriptReference/ Vector3.Lerp.html也許這個教程可以幫助你http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly – yes