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 Tekksin · Oct 25, 2013 at 08:43 AM · gravityjumpingrigid body

HELP!! Rigid Body Jumping Gravity Issue

Hey guys. I'm new to Unity, and more so with coding. I'm making a 2D game and I'm having a bit of trouble with the jumping mechanics.

Initially I used the character preset asset package to control my character, but there was an annoying pill collider in it which didn't work well with this 2D style platformer. So I researched a little and made a code that allows me to control my character without a character controller, and rather a rigid body.

Here is the code:

 #pragma strict
 
 var speed : int = 5;
 var isgrounded : boolean = true;
 
 function Start () {
 
 }
 
 function Update () {
 
     if(Input.GetAxis("Horizontal")){
         transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
     }
     
     if(Input.GetKeyDown("space") && isgrounded == true){
         rigidbody.AddForce(Vector3.up * 500);
     }
 }

Here is a video showing what it does (white background). The blue background is what I WANT the character to jump like. I would go on with using that blue-background-code, but the pill collision on that character controller doesn't work well with what I'm trying to make.

http://www.youtube.com/watch?v=h3L30zcculE&feature=youtu.be

So does anybody know how to tweak the gravity in unity to make the character not only jump up faster, but land fast as well?

I have tried edit > project settings > Input (because there was a gravity option)

I have tried edit > project settings > Physics

The gravity options there don't make the character jump like shown in the blue background example in the youtube video above. So I was wondering if there was a code I could use on a rigid body to modify gravity. Any help would be greatly appreciated.

!!![[[BONUS HELP]]]!!!

As you can see in the video, the character slides around if the jump key ("space") is held down. I don't understand raycasting yet, but I hear that's a way to fix it, to detect that the character has hit the ground. If someone can explain that, or show me a code that works, I'd be very happy.

&

As you can see from the video, if the jump key is continually tapped, the character floats around like kirby, but limitlessly. I would be fine with a double jump, but not an infinite one. How do I limit the amount of times pressing jump takes any effect?

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
0

Answer by Crystalline · Oct 25, 2013 at 08:57 AM

Just: If not jumping and not grounded add a negative force. Attach this script to your rigidbody.

 #pragma strict
 var distToGround: float;
 
 function Start () {
      distToGround = collider.bounds.extents.y;
 }
 
 function IsGrounded(): boolean     
 {
   return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
 }
 
 function FixedUpdate () {
  
 if (IsGrounded())
     {
         // JUMPING 
         if (Input.GetKey(KeyCode.Space))
         {
             transform.rigidbody.AddForce(transform.up* 500);
         }
     
     
 else
         {
             transform.rigidbody.AddForce(transform.up* -500);
         }        
     
     }
 }

  
 
Comment
Add comment · Show 3 · 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 Tekksin · Oct 25, 2013 at 09:53 AM 0
Share

how is unity supposed to know what grounded is?

At the moment, that script makes my character fly away.. I guess my question should be how do I really tell unity that my character is on the ground so that it knows how to respond to these triggers..

avatar image Crystalline · Oct 25, 2013 at 12:01 PM 0
Share

Try that script, attach it to your player rigidbody.

avatar image Tekksin · Oct 25, 2013 at 04:47 PM 0
Share

you can't put a script on rigid body, though....the player holds that attribute so the script has to be on the player, right?

alt text

player.png (129.1 kB)

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

16 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

Related Questions

Double Jump 1 Answer

Character floats upward when looking up and down 0 Answers

Character Controller / Charactor Motor has no inputs? 1 Answer

Why does Gravity (Physics2D) produce different results for AddForce() versus velocity in player sprite? 2 Answers

Grounded Jumping scripting problem. 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