🎮 How to play
|
💡 Code Insight: Physics-based CoBot hovering
The companion character CoBot in BIP follows the player around the level by emmiting a force downwards. CoBot will float at approximately the same distance above any surface it detects below it. We wanted to have a floaty slowish bounce to make the effect more interesting and realistic than just hovering at a set height.
In order to achieve this we set values for
The class is set up so there can be multiple hover points attached to an object
The companion character CoBot in BIP follows the player around the level by emmiting a force downwards. CoBot will float at approximately the same distance above any surface it detects below it. We wanted to have a floaty slowish bounce to make the effect more interesting and realistic than just hovering at a set height.
In order to achieve this we set values for
- the amount of force emmitted downwards
- the intended height of the hovering
The class is set up so there can be multiple hover points attached to an object
How it works
For each hover point, emit a force in the downwards direction from the object.
Derive the force from the intended height and the current height returned from casting a ray from the object down to the nearest surface.
The result is a vector (a magnitude and a direction) which is calculated 50 times a second. Because it is calculated every time FixedUpdate is called (every 0.02 seconds), if the object moves too close to the ground, the force is immediately increased propelling it upwards and if it goes too high the force stops entirely and the native gravity in the game pulls it down again.
For each hover point, emit a force in the downwards direction from the object.
Derive the force from the intended height and the current height returned from casting a ray from the object down to the nearest surface.
The result is a vector (a magnitude and a direction) which is calculated 50 times a second. Because it is calculated every time FixedUpdate is called (every 0.02 seconds), if the object moves too close to the ground, the force is immediately increased propelling it upwards and if it goes too high the force stops entirely and the native gravity in the game pulls it down again.
Build something that makes you think.