Last active
April 14, 2018 13:34
-
-
Save TylerCode/7f9e80b32abe19ee473f084c4a56483b to your computer and use it in GitHub Desktop.
local gravity
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
using UnityEngine; | |
public class LocalGravity : MonoBehaviour | |
{ | |
public Transform m_GravityCenter; | |
private Rigidbody m_RigidBody; | |
void Start() | |
{ | |
m_RigidBody = GetComponent<Rigidbody>(); | |
m_RigidBody.useGravity = false; | |
} | |
void FixedUpdate() | |
{ | |
Vector3 gravityCenter = m_GravityCenter.position; | |
Vector3 gravityDir = gravityCenter - transform.position; | |
gravityDir.Normalize(); | |
m_RigidBody.AddForce(Physics.gravity.magnitude * gravityDir, ForceMode.Acceleration); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment