Desired Result: An object is pulled towards a planet similarly to gravity, without interfering with the rotation of said object.
Problem: Object gets pulled around and rotated.
I create a sphere, then put the main camera as a child of it (this is the player). Then I apply these two scripts to the player:
SpaceLook.js
#pragma strict
var strafe:boolean = true;
var leap:boolean = true;
var speed:int = 10;
var sensativity:int = 1;
function FixedUpdate () {
if (Input.GetKey(KeyCode.W)) {rigidbody.AddRelativeForce(Vector3.forward * speed);}
if (Input.GetKey(KeyCode.S)) {rigidbody.AddRelativeForce(Vector3.forward * -1 * speed);}
if (Input.GetKey(KeyCode.A) && strafe) {rigidbody.AddRelativeForce(Vector3.left * speed);}
if (Input.GetKey(KeyCode.D) && strafe) {rigidbody.AddRelativeForce(Vector3.right * speed);}
if (Input.GetKey(KeyCode.LeftControl) && leap) {rigidbody.AddRelativeForce(Vector3.down * speed);}
if (Input.GetKey(KeyCode.Space) && leap) {rigidbody.AddRelativeForce(Vector3.up * speed);}
if (Input.GetAxis("Mouse X")) {rigidbody.AddRelativeTorque(0, Input.GetAxis("Mouse X"), 0);}
if (Input.GetAxis("Mouse Y")) {rigidbody.AddRelativeTorque(-Input.GetAxis("Mouse Y"), 0, 0);}
if (Input.GetKey(KeyCode.Q)) {rigidbody.AddRelativeTorque(0, 0, 10 * speed * Time.deltaTime);}
if (Input.GetKey(KeyCode.E)) {rigidbody.AddRelativeTorque(0, 0, 10 * -speed * Time.deltaTime);}
if (Input.GetKey("c")) {Application.Quit();}
}
PlanetGravity.js
#pragma strict
var gravity:float = 0.005;
var center:Vector3 = Vector3(0, -177, 0);
var range = 400;
function FixedUpdate() {
rigidbody.AddExplosionForce(-gravity, center, range);
}
The look script doesn’t really matter, but my problem is that when I run this, the sphere keeps spinning slightly from a force I can’t pinpoint. Even when I don’t move my viewport with the mouse at all, it still slightly rotates on it’s local Z and X axii.
Any idea how to fix this?