Lerp.localScale 不工作 OnTriggerEnter,Unity5.3.3,Gear VR App Dev
Lerp.localScale is not working OnTriggerEnter,Unity5.3.3,Gear VR App Dev
我正在开发一个 Gear VR 应用程序,我希望我的角色在进入盒子碰撞器区域时缩小,我可以使用 transform.localScale = new Vector3(0.3F,0.3 F,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");
}
}
你误解了 Lerp 的作用。 Lerp 本身不会对任何东西进行动画处理,它仅通过 t 在两个值之间进行插值,其中 t 在 0 到 1 的范围内。您可以将 t 想象为百分比 -> 如果 t 为 0,则 lerp 的结果是第一个值(或者在 v3 的情况下,在第二个方向上也是 0%),如果 t 为 1,则结果为第二个,如果 t 为 0.5,则结果位于两者的中间,因此如果其为 0.25,则为 25%(Slerp 为基本相同,只是该值表示旋转,请查看维基百科以获取更多信息)。
但这最终对您意味着什么?
需要多次调用Lerp,及时将t设置为你所在的位置。一种选择是启动协程,并在玩家 enters/exits 触发后每帧 in-/decrease t 逐渐 (1 / seconds) * Time.deltaTime
。
编辑:
将其附加到一个可见的游戏对象并 运行 它。它认为它应该弄清楚 Lerp 是如何工作的。重要的部分是t。它需要 in-/decrease 随着时间的推移才能产生动画效果。
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: 好的,这就是协程中的上述 运行ning。不过有一个小 "trap"。我没有考虑玩家在缩放结束前离开触发器的时间。如果他这样做,则持续时间不一致。例如,如果它是 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));
}
}
我正在开发一个 Gear VR 应用程序,我希望我的角色在进入盒子碰撞器区域时缩小,我可以使用 transform.localScale = new Vector3(0.3F,0.3 F,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");
}
}
你误解了 Lerp 的作用。 Lerp 本身不会对任何东西进行动画处理,它仅通过 t 在两个值之间进行插值,其中 t 在 0 到 1 的范围内。您可以将 t 想象为百分比 -> 如果 t 为 0,则 lerp 的结果是第一个值(或者在 v3 的情况下,在第二个方向上也是 0%),如果 t 为 1,则结果为第二个,如果 t 为 0.5,则结果位于两者的中间,因此如果其为 0.25,则为 25%(Slerp 为基本相同,只是该值表示旋转,请查看维基百科以获取更多信息)。
但这最终对您意味着什么?
需要多次调用Lerp,及时将t设置为你所在的位置。一种选择是启动协程,并在玩家 enters/exits 触发后每帧 in-/decrease t 逐渐 (1 / seconds) * Time.deltaTime
。
编辑:
将其附加到一个可见的游戏对象并 运行 它。它认为它应该弄清楚 Lerp 是如何工作的。重要的部分是t。它需要 in-/decrease 随着时间的推移才能产生动画效果。
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: 好的,这就是协程中的上述 运行ning。不过有一个小 "trap"。我没有考虑玩家在缩放结束前离开触发器的时间。如果他这样做,则持续时间不一致。例如,如果它是 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));
}
}