- Home /
Object movement - undesired effects
Hello everyone! I'm new to unity and i'm trying to realize a simple thing: i want to make an object move from point A (a Vector3) to point B (another Vector3 but with a random x value), then when it reaches point B, i would like it to start moving left and right. I've created this code, but it generates two undesired effects: 1) If the values of the random range of the x axis are not set from 0 to 1 (which is a single value) the object in his movement from point A to point B doesn't follow a linear trail, it flickers rapidly and seems to move in various directions at the same time. 2) When the objects reaches point B (the value 0 on the z axis) it is rapidly moved some units to the right, then it starts the correct "PingPong" movement. Can anyone help me?
var enemySpeed : float;
var start : Vector3;
var speed : float;
function Update () {
transform.position = Vector3.Lerp(start, Vector3(Random.Range(-4,4),0,0), Time.time*speed);
if (transform.position.z == 0){
transform.position.x = Mathf.PingPong(Time.time * enemySpeed, 9);
transform.position.z = Mathf.PingPong(Time.time * enemySpeed, 1);
}
}
In another script i have this line of code that instantiates the object, maybe it can help solving the problem:
Instantiate(enemies[0], Vector3(0,0,15), transform.rotation);
The values of this Vector3 and the "start" Vector3 variable are the same.
Answer by whydoidoit · Jul 11, 2012 at 09:37 AM
You are allocating a new random value every Update (every frame) - you need to choose the position to Lerp to in Start and then use it in update. It strikes me that your ping pong is starting from a fixed position so there will be a big jump when it kicks in. You probably want to add the ping pong to the position that was chosen for the Lerp.
I've tryed to change the code as this:
var newpos : Vector3;
function Start(){
newpos = Vector3(Random.Range(-4,4),0,0);
}
function Update () {
transform.position = Vector3.Lerp(start, newpos, Time.time*speed);
if (transform.position.z == 0){
transform.position.x = newpos.x + $$anonymous$$athf.PingPong(Time.time * enemySpeed, 9);
transform.position.z = newpos.z + $$anonymous$$athf.PingPong(Time.time * enemySpeed, 1);
}
}
Now the flickering problem is solved (thank you!) but the strange jump to the right (when the objects reaches point B) still remains. What can i do?
I've converted your answer to a comment - on UA Answer means Solution and not Reply :) There's a hidden Add New Comment button on the right of the screen.
The problem with the jump is that Time.time is greater than 0 when you get to z == 0 (it's the number of seconds since the game started, the way you have it written you will reach newpos after 1 second so you can just take 1 off Time.time in the second bit). The script reference is rubbish on this stuff - really you should maintain your own float variable and add Time.deltaTime to it - as this stops there being problems if you ever want to start this kind of code again or not have it happen instantly as the game runs.
var newpos : Vector3;
float startingTime = 0;
float pingPongTime = 0;
function Start(){
newpos = Vector3(Random.Range(-4,4),0,0);
}
function Update () {
startingTime += Time.deltaTime;
transform.position = Vector3.Lerp(start, newpos, startingTime*speed);
if (transform.position.z == 0){
transform.position.x = newpos.x + $$anonymous$$athf.PingPong(pingPongTime * enemySpeed, 9);
transform.position.z = newpos.z + $$anonymous$$athf.PingPong(pingPongTime * enemySpeed, 1);
pingPongTime += Time.deltaTime;
}
}
Problem solved! Thank you! For now i haven't understood very well this solution, i need some time to study it! :)
Let me see if i've understood. Using Time.time Unity calculates the PingPong position values as the PingPong function would had started from the beginning of the game? So i had to reset time to 0?