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 · May 28, 2016 at 08:49 AM · gravityjump

How to jump while gravity is reversed

My game is in Javascript. My gravity is normally -9.81 in the Y. My jumpheight is normally 9.

I've successfully flipped the gravity when my player/ball touches a special capsule. Unfortunately, I can't get the ball to jump in the opposite direction: down while I'm on the ceiling.

I have been able to change the jump height when I touch other special capsules that grow and shrink the ball (namely make the ball jump higher or lower) but I can't seem to do the above opposite jump.

Here's my code. Any ideas what I'm doing wrong? I've tried different jumpheight values. #pragma strict

 var player : GameObject;
 var gravityChangeSfx : AudioClip;
 
 
 
 var gravityC = Vector3 (0, 9.81, 0);
 
 function Awake() {
     //player = GameObject.Find("Ball").GetComponent.<Rigidbody>();
     player = GameObject.Find("Ball");
 
     Debug.Log("GravityUpsideDown script - first Find Player/Ball");
 } 
 
 
 
 
 function OnTriggerEnter (col : Collider) {
     //Trigger if only the player touched the object
     //GameObject.Find("Player"); 
     if (col.tag == "Player")  {  
             Debug.Log ("GravityUpsideDown script - ball collided with this Box");
             // Change Gravity
 
             Physics.gravity = gravityC;
 
             //Make ball jumpheight the opposite
             BallControl.jumpHeight = -9;
             Debug.Log("GravityUpsideDown script - jumpHeight made upsidedown");
 
 
             GetComponent.<AudioSource>().pitch = 1;
             GetComponent.<AudioSource>().clip = gravityChangeSfx;
             GetComponent.<AudioSource>().Play();
             Debug.Log("GravityUpsdieDown script - gravity upside down");
 
 
 
     }
     
 }


And here's my ball control code associated with the player ball:

 function Update ()
 {
 
     //Handle ball rotation.
     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
     rotation *= Time.deltaTime;
     GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);
 
     //Handle ball jump
     if (Input.GetKeyDown(KeyCode.W) && IsGrounded ())
 
 
 
     {    
         GetComponent.<Rigidbody>().velocity.y = jumpHeight; 
     }
 }
 
 function IsGrounded () : boolean { //Check if we're on the ground, return True if we are, else return Null.
     return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
 }

I thought the problem had to do with the "&& IsGrounded ()" so I commented that out but it still didn't work.

TIA

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
0

Answer by Voodoochief · Jun 08, 2016 at 11:19 PM

First off I would make sure that your Jump is actually being performed. Simply by adding a Debug.Log inside the ball jump statement.

IF that is still being hit I would suggest that you try changing the 'jumpHeight' to '-jumpHeight' to see if that makes the character jump in the desired direction.

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 10, 2016 at 04:55 AM

I must've incorrectly commented out && IsGrounded since it now works correctly.

I just commented out && IsGrounded

Problem now is that I can jump while in mid-air. I need something like IsCeiling ;D

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 Voodoochief · Jun 10, 2016 at 06:24 AM 0
Share

If you change the -Vector3.up, to a Vector3.up when gravity gets reversed you will then detect the ground where it is now (which is above you).

return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1); }

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