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 · Oct 17, 2013 at 09:34 PM · collisionjump

Proper Way to Implement Jump for Character

Hi, I am currently trying to add a jump function to my "PlayerMovement" script. It works fine as far as jumping goes, but I noticed that I can jump on things that I shouldnt be able to(sides of walls, for example)

I know that it has to do with the OnCollisionStay and Exit functions, but I cant figure out what to replace them with to make it work, I have had to resort to using a tag to forbid the player from being able to jump on certain objects, but it makes things more of a hassle than anything

Here is my code! Any help would be greatly appreciated! Thanks again!

 var direction : Vector3 = new Vector3(0,0,0);
 var Speed : float = 0.0;
 var gravity : float;
 var jumpSpeed : float;
 var letJump = false;
 var letMove = true;
 var jumpSound : AudioClip;
 var target : Transform;
 
 function Jump()
 {
     if(letJump)
     {
         if (Input.GetKey(KeyCode.Space))
             {    
                 audio.PlayOneShot(jumpSound);
                 rigidbody.AddForce(0, jumpSpeed, 0);            
             }
     }
 }
 
 function OnCollisionStay(other : Collision)
 {
     if(other.gameObject.tag == "NoJump")
     {
         Debug.Log("Cant Jump Here!");
         letJump = false;
     }
     
     if(other.gameObject.tag != "NoJump")
     {
     letJump = true;
     }
 }
 
 function OnCollisionExit(col : Collision)
 {
     letJump = false;
 }
 
 function Update()
 {
     if (letMove == true)
     {
         direction.x = Input.GetAxis("Horizontal");
         direction.z = Input.GetAxis("Vertical");
     }
     
     direction.Normalize();
     rigidbody.AddForce( direction * Speed );
 
     direction.y -= gravity * Time.deltaTime;
         
     if(Input.GetButtonDown("Jump"))
     {
         Jump();
     }
     
     if(Input.GetButtonUp("Jump"))
     {
         direction.y -= gravity;
     }
 
 }     
 
Comment
Add comment · Show 1
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 Spider_newgent · Oct 17, 2013 at 09:51 PM 0
Share

Hi.

There are loads of ways to do character movement and I'm sure lots of people will do things differently depending on their requirements.

Setting a tag for every object type and using that to filter "letJump" is going to be a pain. Could you maybe place a child object under the players feet and check that for collisions? That way when the object hits a solid surface you could set "grounded = true;" and when you exit the collision, (because you've jumped or walked off a platform) "grounded = false;"

That'll stop your player jumping in mid air/off walls as his feet will have to be on the ground before he can jump again.

Does that help?

3 Replies

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

Answer by Huacanacha · Oct 18, 2013 at 12:01 AM

If you just want to make sure you can only jump of non-vertical surfaces you are colliding with, you can check the collision point normals:

 collision.contacts

to get the contact points, then:

 contactPoint.normal

to get the normals. You can get an average if there are multiple points, or just require a single point to be facing up to allow a jump etc.

However there are a number of issues with using collisions in this way. You may be close but not quite touching the ground so the collider isn't triggered, even though the player thinks they should be able to jump. There may be multiple collisions (ground + wall) that will fight over the let_jump flag.

These issues can be worked around, but overall you're probably better off doing something like the downwards raycast as suggested by others. You can still check the normals of the raycast collision in case 'down' isn't directly down, or if you only want to jump if the angle is < 45 degrees etc. You will need to limit the range of the ray to ensure it doesn't go below your character (with a small amount of tolerance), and you could also use SphereCast with a small radius to ensure you don't have near misses. You can do the 'cast' from the center of the player, or one cast from each foot etc.

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
1

Answer by chillersanim · Oct 17, 2013 at 09:45 PM

As far as I understood you, you want to jump only if the user presses the space bar and some ground is under the avatar.

In this case, you should send a Ray on the negative Y axis (Vector3.down), to check if it collides with something. If yes, then the avatar is on or near the ground and may jump.

You can controll the maximum distance from the ground through the rayDistance. For help with rays, check this article.

Greetings
Chillersanim

Comment
Add comment · Show 2 · 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 · Oct 18, 2013 at 05:12 AM 0
Share

lol funny, I tried this these solutions awhile back and had no luck, tried it once this afternoon and it works like a charm. Never program on a July! lol

avatar image chillersanim · Oct 18, 2013 at 07:08 PM 0
Share

$$anonymous$$aybe the ray in you'r old solution collided with the avatar himself or wasn't the correct length.
If you find this answer helpful, then please accept it, so other users can see it.
I'm happy that I was able to help tough ;)

Greetings Chillersanim

avatar image
0

Answer by danbrummitt · Oct 17, 2013 at 09:50 PM

I can't answer your question with code examples but have you considered ray-casting downward from your character? Then you'd know if there is something "below" the character. This might prevent you jumping off of walls and things that are to the side of you.

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

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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Custom Collision Detection 4 Answers

Simple help with my jackhammer script 3 Answers

How to make an arrow to become stuck? 1 Answer

Character jump on collision 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