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 Bwhang · Jun 10, 2016 at 05:02 AM · gravityjump

How to not jump while in mid-air when the gravity is flipped

Unfortunately, now I can jump in mid-air which I don't want to do.

Is there something like IsCeiling that I can use?

!IsGrounded still lets me jump while in mid-air.

I basically don't want to jump while in mid-air when the gravity is flipped.

Any thoughts?

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

Answer by NoseKills · Jun 10, 2016 at 03:21 PM

All your code is in your previous question. People who see this have no idea what you mean. Lucky for me the questions were still next to each other in the list.

You basically have just one variable changing in your gravity system: the direction of gravity. So you should only have 1 variable to signify it as well. Simpler is better.

  var gravityDir = 1;
  var gravity = Vector3 (0, -9.81, 0);
  var jumpHeight = 9;
 ...
 //when gravity changes, change its sign
 gravityDir *= -1;
 // after that jus use it as a multiplier

  Physics.gravity =  gravityDir * gravity;
  GetComponent.<Rigidbody>().velocity.y = gravityDir * jumpHeight; 
  return Physics.Raycast(transform.position, gravityDir * -Vector3.up, distToGround + 0.1);

This way you don't need any special logic or more variables even if gravity changes back.

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 Bwhang · Jun 25, 2016 at 07:47 AM

Thank you Voodoochief and NoseKills. I'm getting closer to getting this to work using both of your suggestions but it's not 100% yet.

I've got it so that the ball now does jump in the opposite direction (down) when I flip the gravity. However, the test to make sure you're on the ground or ceiling isn't working so you can jump again while in mid-air.

I got to where I am now by making the -Vector3.up into a variable and ditching two parenthesis which I don't quite understand but it helped.

I ditched the "-Vector3.up" in my IsGrounded function and made it the variable (grav) so I could change it from the new default of Vector3.down to Vector3.up when the player/ball touches the special pill that inverts gravity.

 function IsGrounded () : boolean { //Check if we're on the ground, return True if we are, else return Null.
 
         Debug.Log("BallControl script - before IsGrounded raycast " + grav);
         var grav = Vector3.down;
         return Physics.Raycast(transform.position, grav, distToGround + 0.1);



I also ditched two parenthesis "()" after IsGrounded and that made the player/ball jump while on the ground and on the ceiling. Not sure why this worked but it does:

     //Handle ball jump
     //if (Input.GetKeyDown(KeyCode.W) && IsGrounded())  //Ditched the two parenthesis and now it works  
     if (Input.GetKeyDown(KeyCode.W) && IsGrounded)  
 
     {    
         Debug.Log("BallControl script - W was pressed");
         GetComponent.<Rigidbody>().velocity.y = jumpHeight; 
     }

Now I just have to make sure the player/ball can only jump while it is in contact with the ground and/or ceiling and not while in mid-air.

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 Bwhang · Jun 25, 2016 at 08:01 AM 0
Share

Just realized that ditching the two parenthesis negates the ground check so that's why I'm jumping in mid-air. By leaving the parenthesis in, I wasn't jumping at all.

Guess I'll have to add those parenthesis back in.

06/27/2016: Thank you Voodoochief for pointing out that I had a "var" that was unnecessary. Once I ditched that, it all works now. I only jump now when I'm on the ground or ceiling depending on how gravity is flipped.

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

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

Related Questions

How to change the gravity scale of the rigidbody 2d back to it's default value after player lands to the ground from jump 0 Answers

Confused about how AddForce works 1 Answer

Player controller jump not working correctly 0 Answers

Followed Brackey's 1st person movement tutorial but the jump mechanic won't work and sometimes neither will the gravity 0 Answers

How to make a object jump constantly at y and move to the next position to z (perfectly) 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