0

我正在构建一个 Gear VR APP,我希望我的角色在进入盒子碰撞区域时缩小,我可以使用 transform.localScale = new Vector3(0.3F,0.3F,0.3F); 但我希望它能够顺利完成。不知道它为什么不拿起这条 lerp 线的原因?有谁能帮忙吗???我标记了我的盒子对撞机(立方体到“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");
  }

}
4

1 回答 1

0

你误解了 Lerp 是做什么的。Lerp 本身不会为任何东西设置动画,它仅在两个值之间插入 t,其中 t 在 0 到 1 的范围内。您可以将 t 想象为百分比 -> 如果 t 为 0,则 lerp 的结果是第一个值 (或者在 v3 的情况下,在第二个方向上也是 0%),如果 t 为 1,则结果为第二个,如果 t 为 0.5,则结果位于两者的中间,所以如果它的 0.25 为 25%(Slerp 为基本相同,只是该值表示旋转,请查看维基百科以获取有关所有这些的更多信息)。

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

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

编辑:

将此附加到可见的游戏对象并运行它。它认为应该弄清楚 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));
    }
}
于 2016-04-21T14:02:05.977 回答