x


Rotate Gravity 90 degrees for one object.

How do I rotate a player character's gravity 90 degrees on button press? An example of what I want it to do is when you press page down, the gravity of ONE object / character only rotates 90 degrees clockwise. I have tried almost every possible way to get it to work. Please help ASAP. Below is what I currently have as a script that is attached to the character:

var forceToAdd : Vector3 = Vector3(5,5,0);
var obj:GameObject;
private var other:RotateObject;
var rotating : boolean = false;
var changegravityminus :boolean = false;
var changegravityplus :boolean = false;

function Start() {
    other = obj.GetComponent(RotateObject);
}

function Update () {
    if (other.gravity == 1){
       transform.Rotate (0,0,0);
       rigidbody.AddForce (0, 0, 0);
       Physics.gravity = new
       Vector3(0f,-9.81f,0f);
    }

    if (other.gravity == 2){
       transform.Rotate (0,0,90);
       rigidbody.AddForce (0, 0, 0);
       Physics.gravity = new
       Vector3(-9.81f,0f,0f);
    }

    if (other.gravity == 3){
       transform.Rotate (0,0,90);
       rigidbody.AddForce (0, 0, 0);
       Physics.gravity = new
       Vector3(0f,9.81f,0f);
    }

    if (other.gravity == 4){
       transform.Rotate (0,0,90);
       rigidbody.AddForce (0, 0, 0);
       Physics.gravity = new
       Vector3(9.81f,0f,0f);
    }


    if(Input.GetKeyDown (KeyCode.PageUp)&& rotating==false){
       changegravityminus=false;
       rotating=true;
       transform.Rotate (0,0,-90);
       rigidbody.velocity = forceToAdd;
       Physics.gravity =Quaternion.Euler(0, 0, -90) * Physics.gravity;
       rotating=false;
       changegravityminus=true;
    }

    if(Input.GetKeyDown (KeyCode.PageDown)&& rotating==false) {
    rotating=true;
    transform.Rotate (0,0,90);
    rigidbody.velocity = forceToAdd;
    Physics.gravity =Quaternion.Euler(0, 0, 90) * Physics.gravity;
    rotating=false;
    }
}
more ▼

asked Jul 14 '11 at 05:56 PM

ryanbuddy1 gravatar image

ryanbuddy1
1 2 2 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest
if(Input.GetKeyDown("Fire1")){
    yourDifferentGravityGO.rigidbody.useGravity = false;

    //a flag to refer to in fixed update so you can change it's behaviour
    yourDifferentGravityGO.isGravityDifferent = true;

    //then apply a constant force in Fixed Update in the direction you want
}

Turn the default gravity off, apply a constant force.

more ▼

answered Jul 14 '11 at 06:43 PM

Chris D gravatar image

Chris D
2.5k 5 7 25

Sir you deserve a medal, thank you.

Jul 14 '11 at 08:19 PM ryanbuddy1

Glad it helped; good luck!

Jul 14 '11 at 08:21 PM Chris D
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1864
x1089
x1040
x720
x458

asked: Jul 14 '11 at 05:56 PM

Seen: 1006 times

Last Updated: Jul 14 '11 at 08:21 PM