简体   繁体   中英

Vector3.Lerp not working on my camera

I have a cube gameObject with a 2d collider, when someone hit the camera should be up 10 units, but smoothly, camera go to right position but no damping.

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;
}

The reason this is not working is because you are not using Lerp properly. This is how the mathematics of Linear Interpolation (Lerp) work:

Say we have a point (0, 0) and a point (1, 1) . In order to calculate in between points, we provide a t value from 0.0f to 1.0f . This t represents the ratio in between the two points ( 0.0f being (0, 0) and 1.0f being (1, 1) ). For example, the t value of 0.5f would result in the point (0.5f, 0.5f) .

Now expanding on this with a less trivial example, consider two points: var a = Vector2(1.0f, -1.0f) and var b = Vector2(0.0f, 1.0f) . The result of Lerp(a, b, 0.5f) is Vector2(0.5f, 0.0f) because this is the halfway point. Let's call the answer c . The equation that gives us the answer is c = a + (0.5f * (b - a)) . This comes from a basic understanding of linear algebra, where two points subtracted from eachother result in a vector, and adding a vector to a point gives another point.

Okay so now onto makeing this code actually work the way you want it to. Your script could look something like this:

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
    }
}

Hope this helps!

i Set script on trigger, then I add a game object cam, then i drag camera to script , and add cam. in transform.. ty for ur help!! =)

public class 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;
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM