I have a simple 3d environment for playing around and I wanted to add a pausing feature.
Currently when I pause this game I set all game items to “RigidbodyConstraints.FreezeAll”
When I un-pause I set “RigidbodyConstraints.none” however movement of objects do not continue. I am assuming because when the Constraints.FreezeAll is activated all the velocities are set to 0.
How would I resume the velocity is the same direction it was previous traveling.
I have tried saving the velocity of all moving objects and then applying them back to it when unfrozen but have had no luck.
Here is my code:
public void puaseGame()
{
if (round)
{
weaponMovments = new ArrayList();
ballMovments = new ArrayList();
GameObject[] Balls = GameObject.FindGameObjectsWithTag("Ball");
GameObject[] weapons = GameObject.FindGameObjectsWithTag("weapon");
OpenDoors = !OpenDoors;
if (OpenDoors)
{
for(int i =0;i<weapons.Length;i++)
{
weapons[i].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
weapons[i].GetComponent<Rigidbody>().AddForce((Vector3)weaponMovments[i]);
}
for (int i = 0; i < Balls.Length; i++)
{
Balls[i].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
Balls[i].GetComponent<Rigidbody>().AddForce((Vector3)ballMovments[i]);
}
}
else
{
foreach (GameObject weapon in weapons)
{
weaponMovments.Add(new Vector3(weapon.GetComponent<Rigidbody>().velocity.x, weapon.GetComponent<Rigidbody>().velocity.y, weapon.GetComponent<Rigidbody>().velocity.z));
//weaponMovmentsM.Add(weapon.GetComponent<Rigidbody>().velocity.magnitude);
weapon.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
}
foreach (GameObject ball in Balls)
{
ballMovments.Add(new Vector3(ball.GetComponent<Rigidbody>().velocity.x, ball.GetComponent<Rigidbody>().velocity.y, ball.GetComponent<Rigidbody>().velocity.z));
//ballMovmentsM.Add(ball.GetComponent<Rigidbody>().velocity.magnitude);
ball.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
}
}
}
}