- Home /
Smooth Lerp movement?
Currently it just teleports the player to the position of the empty object. How can i make it smoothly move to the empty object?
var touchingLedge : boolean;
var player : GameObject;
var end : Transform;
var start : Transform;
function Update () {
if (touchingLedge == true)
player.transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
function OnTriggerEnter (hit : Collider)
{
if (hit.gameObject.tag == "Player")
touchingLedge = true;
yield WaitForSeconds(0.3);
touchingLedge = false;
}
Answer by greenshadow · May 13, 2013 at 01:06 AM
Here's a solution I ran across some time ago, likely from these boards. Lerp over time. I think it will work as you want it to:
//Declared somewhere near touchingLedge
var moving : boolean = false;
function Update () {
if (touchingLedge == true) {
//Last variable is seconds you want Lerp to last
StartCoroutine(MoveFromTo(start.position, end.position, 0.5f));
}
}
function MoveFromTo(pointA : Vector3, pointB : Vector3, time : float) {
if (!moving) { // Do nothing if already moving
moving = true; // Set flag to true
var t : float = 0f;
while (t < 1.0f) {
t += Time.deltaTime / time; // Sweeps from 0 to 1 in time seconds
transform.position = Vector3.Lerp(pointA, pointB, t); // Set position proportional to t
yield; // Leave the routine and return here in the next frame
}
moving = false; // Finished moving
}
}
Thank you for your response! I will test it out tommorow when i am able. Thank you!
Hello, i get an error It appears to be "yield return 0" I don't know why :
Assets/Standard Assets/Scripts/ClimbWallTransform.js(25,15): UCE0001: ';' expected. Insert a semicolon at the end.
var touchingLedge : boolean;
var player : Transform;
var end : Transform;
var start : Transform;
//Declared somewhere near touchingLedge
var moving : boolean = false;
function Update ()
{
if (touchingLedge == true)
{
//Last variable is seconds you want Lerp to last
StartCoroutine($$anonymous$$oveFromTo(start.position, end.position, 0.5f));
}
}
function $$anonymous$$oveFromTo(start : Vector3, end : Vector3, time : float) {
if (!moving) { // Do nothing if already moving
moving = true; // Set flag to true
var t : float = 0f;
while (t < 1.0f) {
t += Time.deltaTime / time; // Sweeps from 0 to 1 in time seconds
transform.position = Vector3.Lerp(start, end, t); // Set position proportional to t
yield return 0; // Leave the routine and return here in the next frame
}
moving = false; // Finished moving
}
}
function OnTriggerEnter (hit : Collider)
{
if (hit.gameObject.tag == "Player")
touchingLedge = true;
}
@ inglipX: I converted this over to JS from one of my C# projects, and I missed the yield statement. Sorry about that.
Try replacing this:
yield return 0;
With this:
yield;
okay thank you it works. Now how am i able to adjust the speed at which i move?
@inglipX: To change the speed at which you move, you have to figure the duration you want the move to last, and pass that in as the last variable when you call the routine.
So for example, this call moves the object over half a second duration:
//Last variable is seconds you want Lerp to last
StartCoroutine($$anonymous$$oveFromTo(start.position, end.position, 0.5f));
This call would move the object over 2 seconds:
//Last variable is seconds you want Lerp to last
StartCoroutine($$anonymous$$oveFromTo(start.position, end.position, 2f));
So if the distance is going to vary, and you want the speed of the object to always be the same, and the duration to be based on the distance between the two points, you could do something like this:
function Update ()
{
if (touchingLedge == true)
{
//Get the distance between the objects
var distance : float = Vector3.Distance(end.position, start.position);
//Scale the duration down
var duration : float = distance * 0.1f;
//Last variable is seconds you want Lerp to last
StartCoroutine($$anonymous$$oveFromTo(start.position, end.position, duration));
{
{
If you use the last approach, you may have to tinker with this line to get it working how you like it:
//Scale the duration down
var duration : float = distance * 0.1f;
You could move faster by multiplying the distance by a lower number, like this:
//Scale the duration down
var duration : float = distance * 0.05f;
Or you may like it to be a bit slower, by multiplying by a higher number:
//Scale the duration down
var duration : float = distance * 0.2f;
It really all depends how much distance is between the objects in your game, and how fast you want the objects to move.
Your answer
Follow this Question
Related Questions
Animationproblem with Lerp-Function 1 Answer
A node in a childnode? 1 Answer
smooth fps movements using joystick 1 Answer
How to ROTATE an object without slowing the ends (lerp) 3 Answers
Move position with smoothing 0 Answers