Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by rbibler · Feb 17, 2016 at 01:22 AM · jumpingplatformercollision2dmotion

Wall collision slowing vertical jump

Hi All,

I'm developing a 2D platformer that features gameplay similar to Super Mario Bros. or Megaman.

Here's my problem:

When the player runs into a wall, their horizontal motion stops, as you'd expect. But if the player jumps straight up while still in contact with the wall or jumps into the wall while holding down a direction, the player's jump height is reduced by about half of normal.

It's as if the wall is dragging the player down. That said, I don't believe the issue is friction as the problem still occurs if I apply a frictionless material to both the player and the wall surface.

Here are some particulars:

This is a 2D game and I use the Unity physics engine for gravity and vertical motion.

The player's x position is fixed and its x velocity is always 0. The foreground and background scrolls behind the player based on the player's "speed" and direction. In other words, the player doesn't collide with objects, they collide with it.

The player object has a non-kinematic, fixed-angle Rigidbody2D with gravity scale set to 1 and mass of 1000.

The wall has a kinematic, fixed-angle Rigidbody2D and a box collider (its mass is 0, but it doesn't seem to make any difference how I set the mass of either object).

Here is a stripped down version of my character motion controller script, sans animation and other functions:

 // Update is called once per frame
     void Update () {
         vel = transform.rigidbody2D.velocity;
         speed = 0;
         UpdateJumpVel ();
         gameValues.SetSpeed (speed);
         FixXPos ();    
     }
 
     // Lock players X position to account for any collision-induced weirdness
     void FixXPos() {
         transform.position = new Vector3 (fixedXPos, transform.position.y, transform.position.z);
     }
 
     // Called every update
     void UpdateJumpVel() {
         if (jumpOnNext) {
             Jump ();
         }
         transform.rigidbody2D.velocity = vel;
     }
 
     // Start jump with initial jump impulse
     public void Jump() {
         SetState (STATE_JUMPING);
         vel.y = initialJumpImpulse;
         jumpOnNext = false;
     }
 
     void AddToJump() {
         if (noJump || jumpOnNext) {
             return;
         }
         // continually increase vel.y while jump button down.
         // disallow jumping if maxjumptime reached
         if (totalJumpTime > maxJumpTime) {
             noJump = true;
         } else {
             vel.y += maintainJump;
         }
         totalJumpTime += Time.deltaTime;
     }
 
     void UpdateSpeedAndDirection(int newDirection) {
         // Check if player has changed direction and flip sprite if so
         if (newDirection == LEFT && direction == RIGHT) {
             spriteBody.transform.localScale = facingLeft;
             collidedXNormal = 0;
         } else if (newDirection == RIGHT && direction == LEFT) {
             spriteBody.transform.localScale = facingRight;
             collidedXNormal = 0;
         }
         // Set player speed unless player collided with something that opposes  horiz. motion
         if (collidedXNormal != -direction) {
             speed = skateSpeed * newDirection;
         }
         direction = newDirection;
     } 
     
     void OnCollisionEnter2D(Collision2D col) {
         // Always check to see if we've landed
         CheckForBGObjectCollision(col);
     }
 
     // If we've collided with a vertical plane, set contact normal and record object 
     void CheckForBGObjectCollision(Collision2D col) {
         Vector2 contactNormal = col.contacts[0].normal;
         if (contactNormal.normalized.x != 0) {
             collidedXNormal = contactNormal.normalized.x;
             collidedBoundary = col.collider.gameObject;
         }
     }
 
     // Make sure we don't get stuck to the wall!
     void OnCollisionExit2D(Collision2D col) {
         if (collidedBoundary != null && col.collider.gameObject == collidedBoundary) {
             collidedBoundary = null;
             collidedXNormal = 0;
         }
     }

Any help would be greatly appreciated!

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

0 Replies

· Add your reply
  • Sort: 

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

42 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 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

Need Help with Jump Coding 0 Answers

My ground checker only works sometimes 0 Answers

How do I script my 2d platformer player controller so that there is a freedom of a few milliseconds when checking if grounded? 0 Answers

Collision2D or Trigger2D - Jumping Issue 0 Answers

Jumping on enemies help 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