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 Nourero · Apr 02, 2013 at 11:43 AM · c#collidersjumping

How to jump through box colliders but jump on top of them

I need help with a script. I'm currently working on a jumping game and I need help with one part. The character is jumping from platform to platform fine but when he is going more up and comes to a place the platforms are almost blocking the way up he is not able to jump through the colliders. How can I make this happen? For an example see: Sonic jump, Doodle jump etc.

This is my script.

public float movementSpeed = 7; private bool isGrounded = false;

 void Update() {
     rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0); //Set X and Z velocity to 0
 
     transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * movementSpeed, 0, 0);
 }

 void Jump(){
     if (!isGrounded) { return; }
     isGrounded = false;
     rigidbody.velocity = new Vector3(0, 0, 0);
     rigidbody.AddForce(new Vector3(0, 700, 0), ForceMode.Force);        
 }

 void FixedUpdate(){
     isGrounded = Physics.Raycast(transform.position, -Vector3.up, 1.0f);
     if (isGrounded){
         Jump(); //Automatic jumping
     }
 }

Please write in C# thanks.

Comment
Add comment · Show 3
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 robertbu · Apr 02, 2013 at 05:34 PM 0
Share

Some untested ideas: If your game is 2D and especially if you are using an Orhtographic camera, you can change the Z coordinate based on whether your character is rising or falling. So when rising, he will miss all colliders. Or you could disable colliders when he is rising and reenable them when he is falling. Or you could use a plane mesh for your collider rather than a box collider. Since planes are one-sided, he should pass through going up.

avatar image HypoXic5665 · Apr 02, 2013 at 07:05 PM 0
Share

Agreed with robertbu. If there is no reason for you to have to interact with the underside of the object, you can replace the box collider with a plane so that the player can jump 'through' the bottom and land on top. This would be the simplest solution if that is the case you are looking for.

avatar image robertbu · Apr 02, 2013 at 11:23 PM 0
Share

Another simple possibility: turn off the collider on the character when the upward velocity is greater than 0.

1 Reply

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

Answer by AnomalusUndrdog · Apr 02, 2013 at 08:01 PM

Here's how I would do it. When the player presses jump, take the collider of the player, and change its layer to a new one, maybe name it "PassesThroughTerrain". Configure your collision matrix so that that layer does not collide with the terrain's physics layer. http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html

Then, when the player's Y velocity reaches zero or negative, move it back to its usual layer so it won't pass through anymore when it lands.

Also a better way to check for isGrounded is to have code in your OnCollisionEnter that checks the ContactPoint.normal. See if its Y is positive, if so, that means your player collided with something below it. http://docs.unity3d.com/Documentation/ScriptReference/ContactPoint-normal.html

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 Ekta-Mehta-D · Apr 07, 2014 at 05:26 AM 0
Share

Thanks. It works for me..

avatar image KrokolokoXD · Mar 27, 2018 at 02:17 PM 0
Share

Thanks a lot. I was finally being able to create jumpThrough platforms with this.

avatar image Emrys_Q · Sep 15, 2018 at 05:01 PM 0
Share

wow this is REALLY helpful ! thx!!

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to move while jumping? 1 Answer

OnCollisionEnter2D and OnCollisionExit2D 1 Answer

Struggeling with inverselerp and gradients and adding collider to code generated mesh. 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