【问题标题】:Vector3.Lerp not working on my cameraVector3.Lerp 在我的相机上不起作用
【发布时间】:2015-11-04 23:40:23
【问题描述】:

我有一个带有 2d 对撞机的立方体游戏对象,当有人撞到相机时应该向上 10 个单位,但平稳地,相机会转到正确的位置但没有阻尼。

public Transform target;
public float damping = 0.1f;

void OnTriggerEnter2D(Collider2D other)
{
    Vector3 newPoint = Vector3.Lerp(target.transform.position,
                                    target.transform.position + Vector3.up * 10.0f,
                                    damping);

    target.transform.position = newPoint;
}

【问题讨论】:

  • 我想你需要完成other的运动动画,然后。尝试为此启动一个协程,并确保您不会因为相同的碰撞再次启动它。

标签: c# android iphone unity3d


【解决方案1】:

这不起作用的原因是您没有正确使用 Lerp。这就是线性插值 (Lerp) 的数学原理:

假设我们有一个点 (0, 0) 和一个点 (1, 1)。为了在点之间进行计算,我们提供了一个从0.0f1.0ft 值。这个t 表示两点之间的比率(0.0f(0, 0)1.0f(1, 1))。例如,0.5ft 值将导致点 (0.5f, 0.5f)

现在用一个不那么琐碎的例子对此进行扩展,请考虑两点: var a = Vector2(1.0f, -1.0f)var b = Vector2(0.0f, 1.0f)Lerp(a, b, 0.5f) 的结果是 Vector2(0.5f, 0.0f),因为这是中间点。让我们将答案称为c。给我们答案的方程式是c = a + (0.5f * (b - a))。这来自对线性代数的基本理解,其中两个点相减得到一个向量,将一个向量添加到一个点得到另一个点。

好的,现在开始让这段代码真正按照您想要的方式工作。您的脚本可能如下所示:

float moveTime = 10.0f; // In seconds
float moveTimer = 0.0f; // Used for keeping track of time

bool moving = false; // Flag letting us know if we're moving

float heightChange = 10.0f; // This is the delta

// These will be assigned when a collision occurs
Vector3 target; // our target position
Vector3 startPos; // our starting position

void OnTriggerEnter2D(Collider2D other)
{
    if (!moving)
    {
        // We set the target to be ten units above our current position
        target = transform.position + Vector3.up * heightChange;

        // And save our start position (because our actual position will be changing)
        startPos = transform.position;

        // Set the flag so that the movement starts
        moving = true;
    }
}

void Update()
{
    // If we're currently moving and the movement hasn't finished
    if (moving && moveTimer < moveTime)
    {
        // Accumulate the frame time, making the timer tick up
        moveTimer += Time.deltaTime;


        // calculate our ratio ("t")
        float t = moveTimer / moveTime;

        transform.position = Vector3.Lerp(startPos, target, t);
    }
    else
    {
        // We either haven't started moving, or have finished moving
    }
}

希望这会有所帮助!

【讨论】:

  • Jordan Ellis 这个真的在做这个工作,你解释得很好,简单直接。对此有一个问题。好吧,我需要把脚本放在我的相机里,所以我怎样才能让这个触发器成为其他对象触发器。示例:我有 1 个盒子(触发器),我的玩家正在攀爬,当他击中触发器时,我的相机平稳地移动到那里,因此他可以看到更多地图。 (对不起我的英语=P)
  • 你的代码能帮上忙吗?我改变了主意,我想要的基本上是这样的:当玩家碰到我在 MainCamera 中制作的 BoxCollider2D 时会跳,这段代码会发生,但它只工作一次,因为我们需要设置 move = false;再一次,我不知道我该怎么做,我尝试了不同的方法,但没有。
【解决方案2】:

我在触发器上设置脚本,然后我添加一个游戏对象凸轮,然后我将相机拖到脚本,并添加凸轮。在转换中.. 求你的帮助!! =)

公共类 CameraUp : MonoBehaviour {

public GameObject cam;

float moveTime = 10.0f; // In seconds
float moveTimer = 0.0f; // Used for keeping track of time

bool moving = false; // Flag letting us know if we're moving

float heightChange = 10.0f; // This is the delta

// These will be assigned when a collision occurs
Vector3 target; // our target position
Vector3 startPos; // our starting position


void Start()
{


}


void Update()
{

    // If we're currently moving and the movement hasn't finished
    if (moving && moveTimer < moveTime)
    {
        // Accumulate the frame time, making the timer tick up
        moveTimer += Time.deltaTime;


        // calculate our ratio ("t")
        float t = moveTimer / moveTime;

        cam.transform.position = Vector3.Lerp(startPos, target, t);
    }
    else
    {
        // We either haven't started moving, or have finished moving
    }

}

void OnTriggerEnter2D(Collider2D other)
{
    if (!moving)
    {
        // We set the target to be ten units above our current position
        target = transform.position + Vector3.up * heightChange;

        // And save our start position (because our actual position will be changing)
        startPos = cam.transform.position;

        // Set the flag so that the movement starts
        moving = true;
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    相关资源
    最近更新 更多