Skip to content

Instantly share code, notes, and snippets.

@01GOD
Last active November 13, 2016 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 01GOD/134ba1e65f29cd406b25 to your computer and use it in GitHub Desktop.
Save 01GOD/134ba1e65f29cd406b25 to your computer and use it in GitHub Desktop.
UNITY PLANET GRAVITY From http://pastebin.com/YBbFGZzD#
// Unity Tutorial Here: http://youtu.be/TicipSVT-T8
// Sebastian Lague
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
public class GravityBody : MonoBehaviour {
GravityAttractor planet;
void Awake () {
planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<GravityAttractor>();
// Disable rigidbody gravity and rotation as this is simulated in GravityAttractor script
rigidbody.useGravity = false;
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
void FixedUpdate () {
// Allow this body to be influenced by planet's gravity
planet.Attract(transform);
}
}
using UnityEngine;
using System.Collections;
public class GravityAttractor : MonoBehaviour {
public float gravity = -9.8f;
public void Attract(Transform body) {
Vector3 gravityUp = (body.position - transform.position).normalized;
Vector3 localUp = body.up;
// Apply downwards gravity to body
body.rigidbody.AddForce(gravityUp * gravity);
// Allign bodies up axis with the centre of planet
body.rotation = Quaternion.FromToRotation(localUp,gravityUp) * body.rotation;
}
}
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (GravityBody))]
public class FirstPersonController : MonoBehaviour {
// public vars
public float mouseSensitivityX = 250;
public float mouseSensitivityY = 250;
public float walkSpeed = 6;
public float jumpForce = 220;
public LayerMask groundedMask;
// System vars
bool grounded;
Vector3 moveAmount;
Vector3 smoothMoveVelocity;
float verticalLookRotation;
Transform cameraTransform;
void Awake() {
Screen.lockCursor = true;
cameraTransform = Camera.main.transform;
}
void Update() {
// Look rotation:
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime);
verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
// Calculate movement:
float inputX = Input.GetAxisRaw("Horizontal");
float inputY = Input.GetAxisRaw("Vertical");
Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
// Jump
if (Input.GetButtonDown("Jump")) {
if (grounded) {
rigidbody.AddForce(transform.up * jumpForce);
}
}
// Grounded check
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
grounded = true;
}
else {
grounded = false;
}
}
void FixedUpdate() {
// Apply movement to rigidbody
Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
rigidbody.MovePosition(rigidbody.position + localMove);
}
}
clone this paste
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment