- Home /
De-Activating object removes rigidbody velocity
Hello guys!
I got a background which instantiates prefabs, and adds a velocity to them. Since I got several "backgrounds", I deactivate them randomly sometimes. When activating them back, the prefabs that were previously placed do not keep their velocity. Is there anyway to make the rigidbody velocity remain after the re-activation?
Answer by fffMalzbier · Oct 26, 2015 at 05:02 PM
As far as i know there is no build in direct onclick solution. Why not make a small script that stores the velocity on OnDisable() and writhes it back on OnEnable().
I instantiate several images, such as mountains, clouds, trees, etc. I do not keep track of them, I just add velocity and set it to a value. If I disable them, and then re-enable them, the images that were already present have their velocity set at 0, while I'd like them to be the same as previously...
So every single prefab will have such script attached to it? Because I do not see a way to do such thing in the Spawner. The game is mobile, and I am afraid this will have performance issues. At least must be way better than having 5 backgrounds simultaneously working, 4 of them hidden and 1 apparent. So just so I am sure, I just add a script to the object saying: "OnEnable - rigidbody.velocity = velocity" "OnDisable - rigidbody.velocity=0"
You could add the component to each object in code if you prefer to do it that way.
And no, as fff said you'd need to store the velocity in OnDisable, and then use that stored value to set the velocity back in OnEnable. Both your bits of pseudocode are setting the velocity.
The script would not need to run update cycles so there is not performance problem. Storing the velocity in one vector3 should not be a memory problem. Sure you can combine it to one script that manages that all but it's probably unnecessary complexer than the one little script per object.