- Home /
Duplicate Question
Mathf.PingPong from a random position problems
How can i make multiple objects - at a random position in the space - to start moving from where they are to point B (4,0,0) and then back to a point A ( - 4,0,0) using Mathf.PingPong without strange effects?
Let's start. To make an object move from ( - 4,0,0) and not from (0,0,0) to a desired point on the x axis (in this case 4,0,0) the Mathf.PingPong has to be corrected a little, like this:
transform.position = Vector3(Mathf.PingPong(Time.time, 8) - 4, 0, 0);
But this way the various objects in my scene jump all togheter to a single point in space, intersecting with eachother, and then they start moving between -4 and 4. The only way to make the objects start moving from where they are, following separate paths, seems to be adding their transform.position value to the code, like this:
transform.position = transform.position + Vector3(Mathf.PingPong(Time.time, 8) - 4, 0, 0);
But this results in making the objects start travelling really fast along the positive x axis, reaching very high values and disappearing from the game view. To correct this i added a Vector3.Scale using very low values to compensate, like this:
transform.position = transform.position + Vector3.Scale(Vector3(Mathf.PingPong(Time.time, 8) - 4, 0, 0)), Vector3(0.008, 1, 1));
But even this has a terrible effect! it merges objects speed and objects movement range in only one value (you can't control them separately anymore. If you modify 0.008 or 8 or any other value it result in changing both the things). What can i do to make these objects starting their "ping pong movement" along the x axis from their random position in the space, without undesired effects like jumping all together to a single point or start travelling towards very high x values? Please help me!
It's not a time related problem: i already tried to create a time variable and set it to 0 at the moment the PingPong movement starts but the results are the same.
I already answered this here: http://answers.unity3d.com/questions/283498/relative-and-absolute-values-problem.html
Follow this Question
Related Questions
mathf.pingpong 0 Answers
Ping pong position using lerp 3 Answers
Make an object move towards random spot on another objects edge? 1 Answer
instantiate random object in Specified positions? 1 Answer
Keeping the player inside the screen? 2 Answers