Created
March 27, 2017 14:10
-
-
Save keithweaver/ba378a4309508f3cfe8a06531fe92edc to your computer and use it in GitHub Desktop.
Changing Gravity direction on Object in Unity
This file contains 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
// This is for changing gravity direction on the Y axis for | |
// a particular object. | |
// This is for Unity. | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class CharacterMovement : MonoBehaviour { | |
float FORCE_OF_GRAVITY = 9.8F; | |
void Start () { | |
Debug.Log("Start"); | |
} | |
void Update () { | |
// Change gravity on mouse click | |
if (Input.GetMouseButtonDown(0)) { | |
if (Physics.gravity.y > 0) { | |
// Normal gravity so downward force of -9.8 | |
Physics.gravity = new Vector3(0, FORCE_OF_GRAVITY * (-1), 0); | |
} else { | |
// Inverse gravity | |
Physics.gravity = new Vector3(0, FORCE_OF_GRAVITY, 0); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment