Hey everyone, i’m having a few problems w/a waypoint script i’m writing. The script is in 2 parts : Waypoint definition and camera mover.
//Define waypoint properties
var time : float;
var nextWayPoint : GameObject;
var endActor : GameObject;
Really alls i’m doing here is defining a few variables that will help drive the camera.
var currentWayPoint : GameObject;
private var nextWayPoint : GameObject;
function Awake ()
{
transform.position = currentWayPoint.transform.position;
transform.rotation = currentWayPoint.transform.rotation;
}
function Update()
{
movePlayer();
}
function movePlayer()
{
nextWayPoint = currentWayPoint.GetComponent(wayPoint).nextWayPoint;
var lerpTime = nextWayPoint.GetComponent(wayPoint).time;
transform.position = Vector3.Lerp(currentWayPoint.transform.position, nextWayPoint.transform.position, lerpTime);
transform.rotation = Quaternion.Lerp(currentWayPoint.transform.rotation, nextWayPoint.transform.rotation, lerpTime);
}
And in that bit of code i’m moving the camera based on waypoints. The way i want to set it up is to have an initial waypoint that the camera pops to, then get information on the nextWayPoint and use that to transform the camera. The reason i don’t want to use an array of objects is that i need to pause at each waypoint and performs some functions then move after the last object is destroyed.
the problem i’m having right now is that the camera isn’t lerping to the next waypoint, but instead its popping, and its all immediate. So when i run it it goes to the first waypoint then immediately pops to the 2nd waypoint.
I don’t know why it wouldn’t be MOVING to the next waypoint as i’m (hopefully) telling it to transform to that position using the lerp functions.
Any help would be greatly appreciated!
Uh, what exactly are you putting into nextWayPoint.GetComponent(wayPoint).time?
The third parameter Lerp functions receive is a value between 0 and 1. When that value is closer to 0, the return value will be closer to the first parameter entered. When it’s closer to 1, the return value will be closer to the 2nd parameter entered.
If you want a smooth transition, you’ll need to give it a third parameter that will gradually change from 0 to 1.
So, if you want the camera to move from the first waypoint to the second waypoint in two seconds, for example, you can do:
var bMovingCamera: boolean = false;
function Update()
{
if (!bMovingCamera)
{
nextWayPoint = currentWayPoint.GetComponent(wayPoint).nextWayPoint;
StartCoroutine(DoLerp());
}
}
function DoLerp()
{
bMovingCamera = true;
var fStartTime: float = Time.time;
var fLerpLength: float = 2.0;
var fCurrLerp: float = (Time.time - fStartTime) / fLerpLength;
while (fCurrLerp <= 1.0)
{
transform.position =
Vector3.Lerp(currentWayPoint.transform.position,
nextWayPoint.transform.position,
fCurrLerp);
transform.rotation =
Quaternion.Lerp(currentWayPoint.transform.rotation,
nextWayPoint.transform.rotation,
fCurrLerp);
fCurrLerp = (Time.time - fStartTime) / fLerpLength;
yield;
}
bMovingCamera = false;
}
If you want to stick to your code, check the values you are passing to the Lerp function… put Debug.Log(lerpTime);
somewhere in there to see that you’re not giving it values bigger than 1 as the third parameter.
cyb3rmaniak,
Thanks for the post. it helped quite a bit. To answer a your question…
nextWayPoint.GetComponent(wayPoint).time is used to set a varible base time value that can be used in the lerp. I wasn’t doing the math as you noted so it was causing that immediate move to. I want to be able to adjust the time per waypoint.
I have adjusted my code to match closer to yours, i was needing to figure out a way i could cause the loop to pause at the end based on some more parameters and i think what you’ve given is a great baseline.
Thanks again!
Ok so i’ve got my flow setup to work well, moves the way i want and i stop at the “end of the chain” of waypoints.
The problem i’m having now is how to “pause” between waypoints until an action is completed, in this instance when objectX is destroyed.
var bMovingCamera: boolean = false;
var currentWayPoint : GameObject;
private var nextWayPoint : GameObject;
private var progressCharacterDead;
function Update()
{
if (!bMovingCamera)
{
nextWayPoint = currentWayPoint.GetComponent(wayPoint).nextWayPoint;
StartCoroutine(DoLerp());
}
}
function DoLerp()
{
bMovingCamera = true;
progressCharacterDead = false;
var fStartTime: float = Time.time;
var fLerpLength: float = nextWayPoint.GetComponent(wayPoint).time;
var fCurrLerp: float = (Time.time - fStartTime) / fLerpLength;
while (fCurrLerp <= 1.0)
{
transform.position = Vector3.Lerp(currentWayPoint.transform.position, nextWayPoint.transform.position, fCurrLerp);
transform.rotation = Quaternion.Lerp(currentWayPoint.transform.rotation, nextWayPoint.transform.rotation, fCurrLerp);
fCurrLerp = (Time.time - fStartTime) / fLerpLength;
destroyCharacter();
yield;
}
if (progressCharacterDead == true)
{
currentWayPoint = currentWayPoint.GetComponent(wayPoint).nextWayPoint;
if (currentWayPoint.GetComponent(wayPoint).nextWayPoint == null)
{
print ("And now i think the game is over.");
enabled = false;
}
}
bMovingCamera = false;
}
function destroyCharacter()
{
var curChar = currentWayPoint.GetComponent(wayPoint).endActor;
var curWpPosition = currentWayPoint.transform;
if (transform.position == curWpPosition.position)
{
//print ("I am here");
Destroy (curChar);
yield new WaitForSeconds (5);
progressCharacterDead = true;
}
}
I’ve looked a while as a possibility but i’m not sure that will work. Right now what it does is it constantly loops through the first and 2nd waypoint until the 5 second “Wait” is done, then it proceeds again. Not quite what i was hoping for. I need to figure out how i can have it go from waypoint 1 → waypoint 2… wait there until objectX is destroyed, then proceed onto waypoint 3… and i’m really lost righ now.
Thanks everyone and thanks to ypu cyb3rmaniak for the awesome codebit you sent earlier!
No probs man.
And what you want to do is simpler than you would think.
private var progressCharacterDead: boolean = true;
function Update()
{
if (progressCharacterDead !bMovingCamera)
{
nextWayPoint = currentWayPoint.GetComponent(wayPoint).nextWayPoint;
if (currentWayPoint.GetComponent(wayPoint).nextWayPoint == null)
{
print ("And now i think the game is over.");
enabled = false;
}
else
{
StartCoroutine(DoLerp());
}
}
}
function DoLerp()
{
bMovingCamera = true;
// Change progressCharacterDead to be false so that when the camera reaches the next waypoint, it will NOT receive another waypoint until progressCharacterDead is changed back to true.
progressCharacterDead = false;
var fStartTime: float = Time.time;
var fLerpLength: float = nextWayPoint.GetComponent(wayPoint).time;
var fCurrLerp: float = (Time.time - fStartTime) / fLerpLength;
while (fCurrLerp <= 1.0)
{
transform.position = Vector3.Lerp(currentWayPoint.transform.position, nextWayPoint.transform.position, fCurrLerp);
transform.rotation = Quaternion.Lerp(currentWayPoint.transform.rotation, nextWayPoint.transform.rotation, fCurrLerp);
fCurrLerp = (Time.time - fStartTime) / fLerpLength;
yield;
}
// Reached next waypoint.
bMovingCamera = false;
// Now destroy the object, wait for something. Do whatever you want... Although bMovingCamera is now false, until progressCharacterDead is not changed back to true, the camera will not receive a new waypoint.
destroyCharacter();
}
function destroyCharacter()
{
Destroy (curChar);
// Lets say I just want to wait 5 more seconds. Just for kicks...
yield new WaitForSeconds (5);
// OK. Now signal I'm ready to continue
progressCharacterDead = true;
}
Awesome man! Thanks for the update. I’ll have to check it tonight as i’m not at my computer, but this looks really good! Seems as i was doing some things a little backwards?
Thanks again!
Well, not exactly backwards… Just a little sideways
Anyways, It’s not rocket science. After you’ll write your first 20 coroutines the logic of it will become a second nature to you.
Glad to help.