- Home /
applying movement to objects separately
hi everybody.
i've created a move script that allows my game object to move on the scene in x and y directions (it is a 2d project).
i've attached this script to my object's prefab and created another script that spawn this object repeatedly.
i want the objects to literally fall down in the scene (in the "y" direction") and, at the same time, to oscillate on the "x" direction.
i set up the script like this
void Update()
{
movement = new Vector2((Mathf.PingPong(Time.time,2)-1),speedY);
}
the problem is that all the spawned game object oscillate together but i'd like that they oscillate separately (ie. the first spawned object is oscillating according to a, for example, +0.8 value of Mathf.PingPong while the second spawned object is oscillating for a -0.8 value of Mathf.PingPong).
thanks!
Answer by NoseKills · Mar 27, 2014 at 10:54 PM
Perhaps like this ?
const float PINGPONG_LENGTH = 2f;
const float PINGPONG_HALF_LENGTH = PINGPONG_LENGTH / 2f;
float _Offset;
void Start ()
{
_Offset = Random.Range(0, PINGPONG_LENGTH);
}
void Update ()
{
movement = new Vector2((Mathf.PingPong(Time.time + _Offset, PINGPONG_LENGTH) - PINGPONG_HALF_LENGTH), speedY);
}
Actually i think you should use _Offset = Random.Range(0, PINGPONG_LENGTH*2);
otherwise the objects will always start in the same direction, albeit in different phases