- Home /
Add Explosion Force without torque?
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?
Hey $$anonymous$$oon, Did you ever figure this one out? I'm dealing with exactly the same issue.
Answer by aldonaletto · Jan 27, 2013 at 03:14 PM
This is a collateral effect of AddExplosionForce: it applies a torque to the rigidbody proportional to the force. This isn't mentioned in the docs, but I believe that it's an intended feature: AddExplosionForce is supposed to throw things away to simulate explosion effects, and it would be very disappointed if these things didn't spin a lot!
The solution is: don't use AddExplosionForce for anything but explosions. If you want to simulate gravity, replace the second script with something like this:
#pragma strict
var gravity:float = 0.005;
var center:Vector3 = Vector3(0, -177, 0);
var range = 400;
function FixedUpdate() {
var dir = center - transform.position; // find vector ship->planet
var g: float = range / dir.magnitude; // gravity reduces with distance
rigidbody.AddForce(gravity * g * dir.normalized); // apply gravity
}
This isn't 100% physically correct, but is close enough to even make the ship orbit the center position. If you want to implement a real gravity, take a look at this question.
great advice! Thanks for the help. (I'd mark your answer correct if I were the op).