Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lamppost10 · Dec 14, 2012 at 03:40 PM · gravityinvert

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by save · Dec 14, 2012 at 04:00 PM

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);
 }



Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lamppost10 · Dec 14, 2012 at 10:39 PM 0
Share

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

avatar image save · Dec 15, 2012 at 12:29 PM 0
Share

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

avatar image
0

Answer by lamppost10 · Dec 15, 2012 at 01:18 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Move object along waypoints, but affected by gravity -1 Answers

How can i make my object like bouncy on finger move? 1 Answer

Gravity is not working no matter what 0 Answers

How can I find the angle of the direction the velocity is pointing? 1 Answer

Gravity for my AI 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges