- Home /
How to 'Lerp' random local vectors? (Solved!)
I want my character to randomly fly around smoothly. I'm at a loss as to why this doesn't work :(
private var randDir : Vector3;
private var curDir : Vector3;
private var oldDir : Vector3;
private var controller : CharacterController;
function Start() {
controller = GetComponent(CharacterController);
}
function Update () {
if (curDir == randDir){
oldDir = randDir;
randDir.x = Random.Range(-1,2);
randDir.y = Random.Range(-1,2);
}
//print ( "T" + randDir.x + "=" + curDir.x + "T" + randDir.y + "=" + curDir.y + "T" + randDir.z + "=" + curDir.z + " Time:" + Time.time);
//controller.Move(transform.TransformDirection(Vector3.Lerp(Vector3(-.1,0,0), Vector3(0,.1,0), Time.time/10) ));
curDir = Vector3.Lerp(oldDir, randDir, Time.time/10);
controller.Move(transform.TransformDirection(curDir));
//controller.Move(curDir * Time.deltaTime * 20);
print (curDir.x + " " + curDir.y + " " + curDir.z + " " + Time.time);
}
Answer by vished · Jun 28, 2012 at 02:24 PM
Thanks guys - it works great now!
private var curDir : Vector3;
private var oldDir : Vector3;
private var controller : CharacterController;
private var timer : float;
private var curTime : float;
var changeSpeed : float = 2;
var speed : float = 5;
function Start() {
controller = GetComponent(CharacterController);
}
function Update () {
if (curDir == randDir){
//if (Vector3.Distance(curDir, randDir) <= 0.2){
oldDir = randDir;
randDir.x = Random.Range(-1,2);
randDir.y = Random.Range(-1,2);
timer = 0;
}
timer += Time.deltaTime;
curDir = Vector3.Lerp(oldDir, randDir, timer/changeSpeed);
// print ( " T " + randDir.x + " = " + curDir.x + " T " + randDir.y + " = " + curDir.y + " T " + randDir.z + " = " + curDir.z + " Timer"+ timer + "Time:" + Time.time);
local = transform.TransformDirection(curDir)/speed;
controller.Move(local);
print (local.x + " " + local.y + " " + local.z + " " + Time.time);
}
Answer by diddykonga · Jun 28, 2012 at 07:58 AM
Not exactly sure if this is the problem or not, but you might want to try
if (curDir.Distance(randDir) <= 0.2){
cause the chances of them every being equal to each other is one and forever lol XD
Isn't that the purpose of Lerp, to make them equal? I tried:
if (Vector3.Distance(curDir, randDir) <= 0.2){
The first Lerp works as expected but when the values become near equal the new values are generated but the next Lerps don't use the time value and happen every frame rather than every 10 seconds - I wonder why...
Answer by Tim-Michels · Jun 28, 2012 at 08:07 AM
I think your main problem is the fact that you're calculating a new random vector each frame. I would use a Coroutine to calculate a new random direction every x seconds, so you get a more fluent movement.
Now you probably have a lot of jitter on your movement (or very little movement), so I would suggest to recalculate a random direction each 0.5 seconds or so, depending on the kind of movement you want.
Good luck ;)
The Lerp takes 10 seconds to trigger the generation of a new vector ( the first time).
curDir = Vector3.Lerp(oldDir, randDir, Time.time/10);Why is it different the second time? $$anonymous$$aybe you are right, I have to fix this (bug?) by creating a parallel timer for the new vector generation that works in unison with the Lerp timer, or use a yield...?
yield WaitForSeconds(10.0);
Well the third parameter of the Lerp should be a value between 0 and 1.
Since, after 10 seconds, Time.time/10 will be greater than 1, you need to make sure this 'lerp-parameter' stays between 0 and 1.
$$anonymous$$aybe you can add a simple counter, and when the new random vector gets generated, place that timer back to 0.