• 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
Question by StevenMcS · Jun 04, 2014 at 09:18 AM · 2dphysicsrigidbody2djumpingwalls

2D walls affect my jump height

I have created a 2D runner style game. When the player jumps up against a wall there jump height can be increased.

This is how I make the player jump: rigidbody2D.AddForce(new Vector2(0f, jumpForce));

Horizontal speed is constantly added to the character as follows: rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y);

The player is made up of a box collider and a circle collider both of which have no friction.

The walls have a side box collider that is frictionless wherever the player can jump against other wise they will get stuck on the wall.

I have read through a few answers that claim adding a Rigidbody to the wall and enabling its Is Kinematic property will cancel out these affects. http://answers.unity3d.com/questions/420946/why-is-my-jump-height-affected-by-walls.html

I have tried adding a Rigidbody2D to a wall and enabling its Is Kinematic property but the affect seems to be the same.

My current solution is adding small amounts of friction depending on the wall height to reduce the players jump height. This does not produce constant results and slows the player down too much which I do not want.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by PouletFrit · Jun 04, 2014 at 11:13 AM

Since your player already have a rigidbody and your wall a collider, OnCollisionEnter should trigger on your wall, so you just have to add a script on it that will reduce the velocity of your player on the y axis.

You can either directly cut off the velocity or scale it down... depends on your needs. The gravity should take care of the rest.

 public void OnCollisionEnter2D(Collision2D coll) {
     // Add the tag player to your player gameObject
     if (coll.gameObject.tag == "Player") {
         vel = coll.rigidbody.velocity;
         
         // Now reduce the velocity on y axis
         // You may just directly cut if off
         vel.y = Mathf.Max(0, vel.y - reduceJumpingForce);
         // or, scale it down
         vel.y *= 0.5f;
 
         coll.rigidbody.velocity = vel;
     }
 }
Comment
virgiliu

People who like this

1 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 StevenMcS · Jun 04, 2014 at 03:49 PM 0
Share

This works a treat! Just need to do some tweaking :)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Coroutine not working properly 1 Answer

One-way physics reaction : Objects reacting with physics to player without player being affected 2 Answers

Rotate towards velocity (2D) 2 Answers

2D "Platformer" Movement Problems 1 Answer

How can I implement pendulum movement by use Distance Joint 2D? 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