x


Gravity invert not working right...

I am using a script to invert the gravity of a single object when I press the "e" key, but if that object happens to be touching another object at the time, the second object also has its gravity inverted, which I don't want to happen. Here is the script:

var speed = 10; function Update () { if(Input.GetKeyDown("e"))

    if(speed > 0)
    {
        Physics.gravity = Vector3(0, speed, 0);
        speed = -10;
        moveAll();
    }
    else
    {   
        Physics.gravity = Vector3(0, speed, 0);
        speed = 10;
        moveAll();
    }
}

function moveAll() {
var objectsToEffect = GameObject.FindGameObjectsWithTag ("ObjectToEffect"); for (var object in objectsToEffect) {

}

I am not very good at scripting, so this is all pretty foreign to me. I have put the tag "ObjectToAffect" on just the object I want, but it still affects objects touching it.

more ▼

asked Dec 14 '12 at 03:40 PM

lamppost10 gravatar image

lamppost10
1 1 2 3

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

2 answers: sort voted first

Physics is the base class for all physics in your project, meaning you're not addressing any specific rigidbodies. For the objects you want to affect by inverted gravity, use:

rigidbody.AddForce(-Physics.gravity, ForceMode.Acceleration);

Remember to turn off gravity:

rigidbody.useGravity = false;

What you do is reversing the force setup in Physics.gravity (Edit > Project Settings > Physics). Using ForceMode.Acceleration means that you ignore the mass to the continuous applied force.

In your case it would be great to send a message to all these objects that should be affected by inverse gravity,

//Script #1 on each rigidbody with inverse gravity ability
var invertGravity : boolean = false;
function FixedUpdate () {
    if (invertGravity) rigidbody.AddForce(-Physics.gravity, ForceMode.Acceleration);
}
function InvertGravity () {
    invertGravity=!invertGravity;
    if (invertGravity) rigidbody.useGravity=false; else rigidbody.useGravity=true;
}

//Script #2 sending to all rigidbodies with specific tag
private var invertGravityObjects : GameObject[];
function Start () {
    invertGravityObjects = GameObject.FindGameObjectsWithTag("ObjectToAffect");
}

function Update () {
    if (Input.GetKeyDown(KeyCode.E)) SendToAllInversePhysObjects();
}

function SendToAllInversePhysObjects () {
    for (go in invertGravityObjects)
       go.SendMessage("InvertGravity", SendMessageOptions.DontRequireReceiver);
}
more ▼

answered Dec 14 '12 at 04:00 PM

save gravatar image

save
9.5k 22 33 71

thanks, I'll give this a shot! I feel like such a dope now.

Dec 14 '12 at 10:39 PM lamppost10

Don't worry, we all start somewhere! Glad it worked out.

Dec 15 '12 at 12:29 PM save
(comments are locked)
10|3000 characters needed characters left

This is working perfectly! Thank you so much for your help!

more ▼

answered Dec 15 '12 at 01:18 AM

lamppost10 gravatar image

lamppost10
1 1 2 3

(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:

x690
x26

asked: Dec 14 '12 at 03:40 PM

Seen: 927 times

Last Updated: Dec 15 '12 at 12:29 PM