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:
-
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.
-
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.