我正在开发一个Gear VR应用程序,我想让我的角色在进入盒对撞机区域时缩小,我可以通过使用transform.localScale =newVector3(0.3F,0.3F,0.3F)来缩小它的范围;但是我希望它被完成,smoothly.Dont知道为什么它没有接收到这条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");
}
}发布于 2016-04-21 14:02:05
你误解了Lerp的工作。lerp本身没有任何动画,它只在两个值之间插入t,其中t在0到1之间。如果t是0时的百分比->,Lerp的结果是第一个值(如果v3在第二个方向上也是0% ),如果t是1时,结果是第二个,如果t是0.5时,结果位于两者的中间,所以如果它的0.25是25% (Slerp基本上是相同的,只是值代表一个旋转,检查维基百科是否有更多的信息)。
但这对你到底意味着什么?
您需要多次调用Lerp,并将其设置为及时到达的位置。一种选择是启动一个协同线,并在玩家进入/退出触发器后,通过(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:好的,这是在一个协同线中运行的上述内容。不过有个小“陷阱”。我不知道玩家在升级结束前离开扳机的时间。如果他这样做了,那么持续时间是不一致的。例如,如果它的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));
}
}https://stackoverflow.com/questions/36770355
复制相似问题