Created
August 18, 2011 09:01
-
-
Save AngryAnt/1153691 to your computer and use it in GitHub Desktop.
An example of how to easily do an inexpensive alternative gravity setup (untested).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // GravityObject.cs | |
| using UnityEngine; | |
| using System.Collections; | |
| [RequireComponent (typeof (Rigidbody))] | |
| public class GravityObject : MonoBehaviour | |
| { | |
| public float gravityModifier = 1.0f; | |
| void Start () | |
| { | |
| rigidbody.useGravity = false; | |
| GravityWell.AddObject (this); | |
| } | |
| void OnDestroy () | |
| { | |
| GravityWell.RemoveObject (this); | |
| } | |
| } | |
| // GravityWell.cs | |
| using UnityEngine; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| public class GravityWell : MonoBehaviour | |
| { | |
| public float gravity = 9.8f, radius = 1.0f; | |
| private static List<GravityObject> objects = new List<GravityObjects> (); | |
| public static void AddObject (GravityObject newObject) | |
| { | |
| objects.Add (newObject); | |
| } | |
| public static void RemoveObject (GravityObject existingObject) | |
| { | |
| objects.Remove (existingObject); | |
| } | |
| void OnApplicationQuit () | |
| { | |
| objects = new List<GravityObjects> (); | |
| } | |
| void FixedUpdate () | |
| { | |
| foreach (GravityObject target in objects) | |
| { | |
| target.rigidbody.AddExplosionForce (-gravity * target.gravityModifier, transform.position, radius, 0.0f, ForceMode.Acceleration); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment