Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Sdavies93 · Mar 17, 2016 at 04:55 PM · scripting problemgravitybooleantoggle

Toggle Gravity on button press

I'm creating a side scroller where the Gravity of the player is a gameplay mechanic.

The initial gravity value is negative (falling downwards) and when the player presses the G key, the gravity changes to positive (floating upwards).

The script toggles a boolean value on/off when the G key is pressed. This part of the code works.

If the boolean is false(off) then the gravity should be -

if the boolean is true (on) then the gravity is equal to a antigrav variable (+gravity).

I can get the gravity to toggle so that the players gravity changes to positive, but I cannot change it back. Even though the boolean variable continues to toggle on/off.

 [RequireComponent (typeof (Controller2D))]
 public class Player : MonoBehaviour {
 
     public float jumpHeight = 4;
     public float timeToJumpApex = .4f;
     float accelerationTimeAirborne = .2f;
     float accelerationTimeGrounded = .1f;
     float moveSpeed = 6;
 
     public bool antigravTrue = false;
     float antigrav;
     float gravity;
     float jumpVelocity;
     Vector3 velocity;
     float velocityXSmoothing;
 
     Controller2D controller;
 
     void Start () {
         controller = GetComponent<Controller2D> ();
         gravity = -(2 * jumpHeight)/Mathf.Pow (timeToJumpApex, 2);
         antigrav = (2 * jumpHeight)/Mathf.Pow (timeToJumpApex, 2);
         jumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
         print ("Gravity: " + gravity + " Jump Velocity: " + jumpVelocity);
 
     }
 
     void Update(){
 
         if (controller.collisions.above || controller.collisions.below){
             velocity.y = 0;
         }
 
         Vector2 input = new Vector2(Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
 
         if (Input.GetKeyDown(KeyCode.Space) && controller.collisions.below){
             velocity.y = jumpVelocity;
         }
 
         if (Input.GetKeyDown(KeyCode.Space) && controller.collisions.above){
             velocity.y = -jumpVelocity;
         }
 
         if(Input.GetKeyDown(KeyCode.G)){
             antigravTrue = !antigravTrue;
 
         }
 
         if(antigravTrue){
             gravity = antigrav;
         } 
 
         if(!antigravTrue){
             gravity = gravity;
             print ("its false");
         }
 
     
 
         float targetVelocityX= input.x * moveSpeed;
         velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing,(controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
         velocity.y += gravity *Time.deltaTime;
         controller.Move(velocity * Time.deltaTime);
     }
 }

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

1 Reply

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

Answer by Bonfi_96 · Mar 17, 2016 at 08:39 PM

That's because you're setting back gravity equals to itself, so it's value won't change.

Let's say gravity = 10 and antigrav = -10 After gravity = antigrav; both your values would be equal to -10 cause this line of code says "set gravity value to antigrav's value"

Then you try to set it back with gravity = gravity; but that's like saying "gravity = -10" as in the line before you set gravity to this value.

A quick solution would be to add a "currentGravity" value so you won't overwrite the gravity variable

Comment
Add comment · Show 1 · 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 Sdavies93 · Mar 18, 2016 at 09:57 AM 0
Share

Ah that makes total sense. It seems so simple now.

Thanks for helping .

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

54 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to enable/disable an Icon depending on the bool state of a toggle. 0 Answers

Script Variable not transfering 0 Answers

No Gravity Jump 0 Answers

Disable light not working 0 Answers

How can I add a toggle to this FPS counter script? 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