Lerp.localScale不支持OnTriggerEnter,Unity5.3.3,Gear VR App Dev

时间:2016-04-21 12:57:55

标签: javascript triggers unity5 gear-vr lerp

我正在建造一个Gear VR APP,我想让我的角色在进入箱子对撞机区域时缩小,我可以通过使用transform.localScale = new Vector3(0.3F,0.3F,0.3F)突然缩小它);但是我希望它能顺利完成。不知道为什么它没有拿起这条lerp线?可以任何一个helpp ???我标记我的盒子对撞机(立方体到#34; Mani")和一件事更多,OnTriggerExit我的lerp也没有打电话。

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

}

1 个答案:

答案 0 :(得分:0)

你误解了Lerp的作用。 Lerp本身没有动画任何东西,它只在两个值之间插入t,其中t在0到1的范围内。你可以想象t为百分比 - >如果t为0,则lerp的结果为第一个值(或者v3的情况也是第2个方向的0%),如果t为1则结果为第二个,如果t为0.5,则结果位于中间两者都是,所以如果它的0.25为25%(Slerp基本相同,只有该值代表一个旋转,请检查维基百科以获取更多信息)。

但这对你最终意味着什么呢?

您需要多次调用Lerp,并将t设置为您的时间点。一个选项是在玩家进入/退出触发器时启动协程并逐渐减少/减少每帧(1 / seconds) * Time.deltaTime

编辑:

将其附加到可见的游戏对象并运行它。它认为它应该清理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:好吧,这是以上在协程中运行的。虽然是一个小“陷阱”。我没有说明玩家在缩放结束前离开触发器的时间。如果他这样做,则持续时间不一致。例如,如果它是10s并且他在2s后离开,他将缩回到正常超过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));
    }
}