Adapting gravity for Unity3D
Think of games like super mario galaxy. Mario can jump from one planet to another and run on those small spheres without problems. Also in racing games the tracks are often not just on the ground. With this repository I want offer a small library to create exactly such gravity adaption possibilities.
To get the desired effect all objects that should adapt to gravity check for ground objects via ray-casts. If they find a ground object near them, they save the normal map of the surface and take the negative surface normal as the new gravity direction.
To use the library you have to make changes for every object that should be attracting as well as for every object that should be influenced by gravity. The introduction of adapting gravity can be accomplished by those few steps:
- Add the AdaptingGravity script to all objects that should attracted by adapting gravity. You can also start by only adding it to your player. You find the script in Deml/Physics/Gravity. If you are starting your project from scratch you can add the prefab player from Deml/Player/Prefabs/Player.
- Once the script is on the player you can tweak specific settings for it.
- The attracting object tags will define which tags should be considered to be attracting to the player.
- The gravity strength defines the strength the player will be attracted to objects. The acceleration is set in m/s². (Side note: The gravity strength of the earth would be 9.81 m/s², but in games you would normally use a greater strength)
- The gravity check distance defines the distance of the ray that will check of objects that are able to attract the object
- The ground check distance defines the distance for which an object will be seen as touching the ground. This could be relevant if you want to only be able to move with the player if it is touching the ground.
- If you want the player to perfectly rotate to the current gravity direction also add the script LockFallingOver to the player object. It can be found in AdaptingGravity/Scripts/Physics/Manipulation
- Add one of the tags you defined are valid for attracting objects (You defined them in step 2.1, if you didn't the standard value is only the tag "Ground") to all objects you want to be attracting to other objects.
- You can where the evaluation of ground objects should happen by adding handles to the object. You will find a button for that in the GravityHandler script.
- With the weigted average toogle you can define if the gravity should be defined by the direction of the nearest object that is hit by a ray or by the average of all objects that are hit.
- You're set and ready to tweak the settings to your needs.
AdaptingGravity is released under the MIT Licence.