I haven’t had much of a chance to work with physics in Unity and find the prospect quite intimidating, but I think this would be a great opportunity to learn the basics and would add some extra movement to our game to help it feel more alive.
What we want to achieve:
- A realistic drawer containing spheres (glass eyeballs) that will simulate physics and roll around with the force of opening and closing the drawer.
Adding “walls” to the inside of a drawer:
To start, we will need to essentially line the inside of our drawer model with invisible colliders that will interact with the spheres and stop them from moving through and out of the model.
We can also then adjust the thickness or position of the colliders to ensure the area in which the spheres can move matches where we can see and reach during runtime.
After doing this I add our first “eyeball”, a.k.a. a sphere to the drawer and give it a rigid body with gravity.
Obviously, this doesn’t work as intended. The sphere does move with the drawer, which is good, and it also isn’t clipping through the bottom of it and falling off into the void, which is also good, but there’s no movement at all, which isn’t the effect we want.
I then learnt that in order for objects to receive physics updates during run time, they need to also have a rigid body attached with the “isKinematic” box checked.
This means movement won’t apply to the object unless done so through code or the inspector (which stops objects from immediately falling downwards on play).
After making this change, we can see that there definitely is gravity being applied to our sphere, and that changing the rotation of the drawer is having an effect, but the sphere still doesn’t move in any way when the xyz transform of the drawer is changed.
From what I understand, it could be that in order to properly use Unity’s physics system, I need to have a better understanding of real-world physics and how this situation would actually work.
It could be that because the drawer movement script works on changing the transform of the game object depending on the mouse position, no forces are being applied to the drawer, and so no force is being applied to the sphere, hence no physics movement.
It could also be that the sphere is a child object of the drawer, which is what causes it to move with the drawer, and this is preventing any movement in the xyz.
By making the sphere a sperate object, it’s movement will be caused by the colliders of the drawer hitting it, getting something closer to the effect we want, but it moves very slowly and can potentially clip outside of the drawer which is a problem.
It seems like the best solution might be to “fake” the effect of physics on the objects, by applying a force to the eyes in a specific direction through script when the drawer is moved, using the distnace/speed moved to control the strength of the force. We can also randomise the direction slightly (within a range of values) to help make it look more scattered.
I also found when I made all the colliders lining the drawer part of one object within the prefab, the isKinetic property on the eyeball seemed to have the opposite effect, needing to be unchecked in order for physics to apply when moving the object through the inspector.
To prevent the objects in the drawer from clipping through and falling out at higher speeds, I set the collision detection mode to “continuous”. This can impact the speed/performance of the game, so luckily this is the only time we really plan to use anything like this.
Laura Alford