how to control instances velocity?
I have a Rigidbody 2d ,which i am invoking in a function and repeatedly it`s instances are being created.I am assigning the instances to three different Rigidbody2d variable(as i am only using 3 instances in one screen) At the time of initialisation i am setting the velocity of each instance to a certain number and it is working fine until a certain condition is invoked,which i have mentioned in the update function. In the update function i am changing each instances`s velocity differently,but after a while but all the instances are having the same velocity .The velocity they are ending up having is the velocity of the last Rigidbody2d variable in the update function whose velocity i am changing. Please help.
here is my update function: void Update() {
pos = tray.position;
postray = traycopy.position;
postray2 = traycopy2.position;
Debug.Log(postray);
if (check1 == true)
{
if (postray2.x <= -2.6)
{
traycopy2.velocity = new Vector2(8.5f, 0);
}
if (postray2.x >= 2.6)
{
traycopy2.velocity = new Vector2(-8.5f, 0);
}
}
if (check1 == true)
{
if (postray.x <= -2.6)
{
traycopy.velocity = new Vector2(4.5f, 0);
}
if (postray.x >= 2.6)
{
traycopy.velocity = new Vector2(-4.5f, 0);
}
}
if (check1 == true)
{
if (pos.x <= -2.6)
{
tray.velocity = new Vector2(2.5f, 0);
}
if (pos.x >= 2.6)
{
tray.velocity = new Vector2(-2.5f, 0);
}
}
}
Here is my initiate function:
void trayrepeat()
{
if (traycontrol == true)
{
traycontrol = false;
random1 = Random.Range((float)(-2.5), (float)(2.5));
if (i== 1)
{
Rigidbody2D traycopy = Instantiate(tray2, new Vector2(random1, x), Quaternion.identity) as Rigidbody2D;
pos2 = traycopy.position;
// Debug.Log(pos2);
velocityrandom = Random.Range(1, 5);
traycopy.velocity = new Vector2(-4.5f, 0);
x = (float)(x + 4);
}
if (i == 2)
{
Rigidbody2D traycopy1 = Instantiate(tray2, new Vector2(random1, x), Quaternion.identity) as Rigidbody2D;
pos2 = traycopy1.position;
// Debug.Log(pos2);
velocityrandom = Random.Range(1, 5);
traycopy1.velocity = new Vector2(-8.5f, 0);
x = (float)(x + 4);
}
if (i == 3)
{
Rigidbody2D traycopy2 = Instantiate(tray2, new Vector2(random1, x), Quaternion.identity) as Rigidbody2D;
pos2 = traycopy2.position;
// Debug.Log(pos2);
velocityrandom = Random.Range(1, 5);
traycopy2.velocity = new Vector2(-2.5f, 0);
x = (float)(x + 4);
}
i++;
if (i==4)
{
i = 1;
}
}
Your answer
Follow this Question
Related Questions
Object not moving even after having velocity 0 Answers
Inconsistent Velocity? 0 Answers
Help! My projectile velocity is not working.. 1 Answer