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 Orggrim · May 14, 2013 at 08:43 PM · collisionplayerjumpjumpingdetection

Detecting Collision for Jumping

Hi, I was looking to see how I would detect collision between two objects, my player and the floor.

Im trying to give my player the ability to jump when they press the space bar, I tried this out at first

 if (Input.GetKey(KeyCode.Space))
 {
     rigidbody.AddForce(0, 10, 0);                        
 }

But this makes it to where the player flies up continuously if pressed.

What I thought about doing was detecting whether the player was colliding with the floor, and then if true, letting him jump.

Hopefully it would let him go up,then detect that the player was not colliding with the floor, then send him back down.

Please let me know if you need any further info, or if there is a simpler way to do this

Thanks again!

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

Answer by gharbill · May 14, 2013 at 08:55 PM

Use function OnCollisionEnter(). your code should be something like this:

 var letJump : boolean;
 
 OnCollisionEnter(col:Collision){
 
 if (col.transform.tag == "Ground") letJump = true;
 
 }
 
 function Update (){
 
     if (Input.GetKey(KeyCode.Space))
     {
      if (letJump){
          rigidbody.AddForce(Vector3(0, 10, 0),ForceMode.Impulse);
          letJump = false;
     }
 }
Comment
Add comment · Show 5 · 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 Orggrim · May 14, 2013 at 09:57 PM 0
Share

so I would just label all my floor objects with the tag Ground, then when my player touches anything with that tag, it changes letJump = true, am I on the right track?

avatar image gharbill · May 14, 2013 at 10:08 PM 0
Share

yes that works but I was just showing a way for your solution. depending on your game this may or may not be good Idea. for example you want your player to be able to jump when standing on top of an object, then you should change the tag of every jumpable object to "ground" which is kind of nonsense. If i were you i would have created a leg collider for my player which was staying just under the player body. and when ever this collider hits something it means our player has landed somewhere, it doesn't matter on a ground or somewhere else. this way you could omit the tag checking "if". hope this helps :)

avatar image gharbill · May 14, 2013 at 10:12 PM 0
Share

oh by the way,the leg collider is better to be set to trigger (checking trigger box in the inspector), and then you should use OnTriggerEnter(col : Collider) function ins$$anonymous$$d of OnCollisionEnter(col : Collsion)

avatar image Orggrim · May 15, 2013 at 02:12 AM 0
Share

thanks for your help! I just removed the tag check and changed a little bit of my script into functions, works like a charm now, THU$$anonymous$$BS UP!!

avatar image gharbill · May 15, 2013 at 02:47 AM 0
Share

youre welcome. just don't forget to choose the answer as correct if it is :)

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

14 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

Related Questions

Can't figure out how to detect ground (2D) 1 Answer

Problem between my player jump and player movement 1 Answer

Having a problem with player jumping 1 Answer

Character Fails To Jump Sometimes 1 Answer

Player wont jump (visual script),2d player does not jump (visual 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