• 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 herp johnson · Jul 26, 2012 at 03:37 AM · gravityoff

Gravity increasing more even though it's turned off

I have a helicopter that uses a transform script to move, and I coded it so that it only uses gravity when the down key is pressed (any other time it just hovers). However, when I hold or press the down key, gravity stays on after I let go. The check box in rigidbody stays off, though. Is there any reason this happens or is it just some obscure scripting in unity?

CODE (I'm a novice coder so I apologize for any redundancies that make it hard to read)

 #pragma strict
 var up : boolean = false;
 var down : boolean = false;
 var left : boolean = false;
 var right : boolean = false;
 var strafefwd : boolean = false;
 var strafeback : boolean = false;
 var straferight : boolean = false;
 var strafeleft : boolean = false;
 
 //var rotorScript: rotors = GetComponent(rotors);
 //var rotors :rotors = GetComponent(FrontRotor);
 
 var rotors:Transform;
 function Start () {
 
 //keeping physics from interfering with rotation
  rigidbody.freezeRotation = true;
 }
 
 function Update () {
 
 //reading input/setting booleans
 if (Input.GetKeyDown("up"))
  {up = true;}
 else if (Input.GetKeyUp("up"))
  {up = false;}
 else if (Input.GetKeyDown("down"))
  {down = true;}
 else if (Input.GetKeyUp("down"))
  {down = false;}
 else if (Input.GetKeyDown("w"))
  {strafefwd = true;}
 else if (Input.GetKeyUp("w"))
  {strafefwd = false;}
 else if (Input.GetKeyDown("s"))
  {strafeback = true;}
 else if (Input.GetKeyUp("s"))
  {strafeback = false;}
 else if (Input.GetKeyDown("a"))
  {strafeleft = true;}
 else if (Input.GetKeyUp("a"))
  {strafeleft = false;}
 else if (Input.GetKeyDown("d"))
  {straferight = true;}
 else if (Input.GetKeyUp("d"))
  {straferight = false;}
 else if (Input.GetKeyDown("left"))
  {left = true;}
 else if (Input.GetKeyUp("left"))
  {left = false;}
 else if (Input.GetKeyDown("right"))
  {right = true;}
 else if (Input.GetKeyUp("right"))
  {right = false;}
  
 //movement 
 // up and down
 if (up)
 {
  rigidbody.useGravity = false;
    if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(0, 2, 0);
 }
 if (down)
 {
  rigidbody.useGravity = true;
  Physics.gravity = Vector3(0, -1.0, 0);
  if(rotors.hingeJoint.motor.force > 500)
   transform.Translate(0, 0, 0);
 }
 else
  rigidbody.useGravity = false;
 
 //turn left/right
 if (left)
 {
  rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Rotate(0, -2, 0);
 }
 if (right)
 {
  rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Rotate(0, 2, 0);
 }
 
 //fwd and back
 if (strafefwd)
 {
  rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(0, 0, 2);
 }
 if (strafeback)
 {
  rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(0, 0, -2);
 }
 
 //left and right
 if (strafeleft)
 {
  rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(-2, 0, 0);
 }
 if (straferight)
 {
  rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(2, 0, 0);
 }
 
 
 
 }
Comment
Add comment · Show 2
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 Seth-Bergman · Jul 26, 2012 at 03:47 AM 0
Share

without the code, there's no telling what's going on.. post the relevant code

avatar image herp johnson · Jul 26, 2012 at 04:08 AM 0
Share

ok i updated it

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by herp johnson · Jul 26, 2012 at 09:07 AM

I'm not a very experienced coder so I apologize if it's hard to read

 #pragma strict
 var up : boolean = false;
 var down : boolean = false;
 var left : boolean = false;
 var right : boolean = false;
 var strafefwd : boolean = false;
 var strafeback : boolean = false;
 var straferight : boolean = false;
 var strafeleft : boolean = false;
 
 //var rotorScript: rotors = GetComponent(rotors);
 //var rotors :rotors = GetComponent(FrontRotor);
 //var Y : float = transform.position.y;
 var rotors:Transform;
 function Start () {
   //keeping physics from interfering with rotation

   rigidbody.freezeRotation = true;
 }
 
 function Update () {


 //reading input and setting booleans
 if (Input.GetKeyDown("up"))
  {up = true;}
 else if (Input.GetKeyUp("up"))
  {up = false;}
 else if (Input.GetKeyDown("down"))
  {down = true;}
 else if (Input.GetKeyUp("down"))
  {down = false;}
 else if (Input.GetKeyDown("w"))
  {strafefwd = true;}
 else if (Input.GetKeyUp("w"))
  {strafefwd = false;}
 else if (Input.GetKeyDown("s"))
  {strafeback = true;}
 else if (Input.GetKeyUp("s"))
  {strafeback = false;}
 else if (Input.GetKeyDown("a"))
  {strafeleft = true;}
 else if (Input.GetKeyUp("a"))
  {strafeleft = false;}
 else if (Input.GetKeyDown("d"))
  {straferight = true;}
 else if (Input.GetKeyUp("d"))
  {straferight = false;}
 else if (Input.GetKeyDown("left"))
  {left = true;}
 else if (Input.GetKeyUp("left"))
  {left = false;}
 else if (Input.GetKeyDown("right"))
  {right = true;}
 else if (Input.GetKeyUp("right"))
  {right = false;}
 
 //using booleans for movement
 
 // up and down
 if (up)    
 {
  rigidbody.useGravity = false;
    if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(0, 2, 0);
 }
 if (down)
 {
     rigidbody.useGravity = true;
  if(rotors.hingeJoint.motor.force > 500)
   transform.Translate(0, 0, 0);
 }
 else
 rigidbody.useGravity = false;


 //turn left/right
 if (left)
 {
     rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Rotate(0, -2, 0);
 }
 if (right)
 {
     rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Rotate(0, 2, 0);
 }
 
 
 //fwd and back
 if (strafefwd)
 {
     rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(0, 0, 2);
 }
 if (strafeback)
 {
     rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(0, 0, -2);
 }
 
 //left and right
 if (strafeleft)
 {
 rigidbody.useGravity = false;
  if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(-2, 0, 0);
 }
 if (straferight)
 {
     rigidbody.useGravity = false;
     if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(2, 0, 0);
 }
 
 
 
 }
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
avatar image
0

Answer by Seth-Bergman · Jul 26, 2012 at 04:24 AM

Well, the problem is once you turn off gravity your object still has momentum, and so it keeps going.. you would have to adjust for its downward momentum. Honestly, You may as well just stick to transform and forget about gravity altogether, if all you're doing is using it to go down.

Also, there's no need for all the top stuff, just this will do the same thing:

 if (Input.GetKey("up"))
 {
  rigidbody.useGravity = false;
    if(rotors.hingeJoint.motor.force > 500)
    transform.Translate(0, 2, 0);
 }
 else if (Input.GetKey("down"))
 {
  rigidbody.useGravity = true;
  Physics.gravity = Vector3(0, -1.0, 0);
  if(rotors.hingeJoint.motor.force > 500)
   transform.Translate(0, 0, 0);
 }
 else if (Input.GetKeyUp("down"))
  rigidbody.useGravity = false;

etc...

yeah, if you want the physics intact, try rigidBody.AddForce() in place of transform.Translate that should work better. I'd forget about gravity and just use that.

Comment
Add comment · Show 7 · 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 herp johnson · Jul 26, 2012 at 04:27 AM 0
Share

but that allows you to to go through things. Is there code for stopping it when it detects collision?

avatar image Seth-Bergman · Jul 26, 2012 at 04:35 AM 0
Share

see edit.. I would try rigidBody.AddForce()

avatar image herp johnson · Jul 26, 2012 at 04:38 AM 0
Share

ok thanks! :)

avatar image Seth-Bergman · Jul 26, 2012 at 04:44 AM 0
Share

of course if you really don't care about collision EXCEPT when you go down, the easiest fix is to AddForce up when you stop pushing down, just enough to counter the momentum.. And if you don't $$anonymous$$d stopping on a dime, easier still is to just set is$$anonymous$$inematic = true; that would stop you cold..

is$$anonymous$$inematic means you ONLY move via transform, so that would stop the physics..

avatar image herp johnson · Jul 26, 2012 at 05:15 AM 0
Share

well that addForce in place of translate didnt do anything..

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Having trouble turning off gravity through script 1 Answer

turning off gravity for a controller 3 Answers

Gravitational pull without losing speed 1 Answer

RigidBodys Suddenly Heavier 1 Answer

Turn Gravity on when collision occurs 1 Answer


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