Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Created March 27, 2017 14:10
Show Gist options
  • Save keithweaver/ba378a4309508f3cfe8a06531fe92edc to your computer and use it in GitHub Desktop.
Save keithweaver/ba378a4309508f3cfe8a06531fe92edc to your computer and use it in GitHub Desktop.
Changing Gravity direction on Object in Unity
// 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