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 Helipork · Mar 20, 2015 at 05:31 AM · physicsjumpplatformer

Collision between CircleCollider2D and BoxCollider2D causing physics problems

Hello,

I am currently developping a small 2D platformer, in which my character is a simple circle, with a simple CircleCollider2D.

That character moves in a really basic 2D environment made of squares and rectangles, all of which have simple BoxCollider2D's.

For jumping, I am using a simple AddForce based on how long you keep "space" pressed, so that the player can somewhat control the height of jumps, up to a certain threshold.

Here is my problem: when the character jumps and collides in mid-air with the angle of a BoxCollider2D, that collision gives the jump a huge boost! It's as if the collision between Collider2D's amplified the jump itself. Note that the AddForce is not applied again when the player collides with another object, and the vertical force applied still maintains the same cap.

Has anyone ever experienced something similar? Because if not that might aswell be something wrong with my scripts, in which case I'd just start over.

Just wanted to check with the community before I erased my character controller, since I haven't found a thread about this subject!

Thank you for your time, best regards!

EDIT: Here is the first script. I know it looks terrible, sorry for that! Everything works just fine though, except that strange boost when hitting angles. (The script parts wrapped between comments mean these lines have nothing to do with the jumping).

Here it is:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     //Can the player wall jump? (after completing a certain level)
     public bool wallJump = false; 
     public bool jumping = false; //Is the player jumping?
     public float maxSpeed = 80.0f; //Player movespeed
     //Base force applied to the player when jump button is first pressed
     public float baseJumpForce = 60.0f;
     public float currentJumpForce = 0; //Currently applied jump force
     //Maximum jump force (the jump stops after reaching this threshold)
     public float maxJumpForce = 300.0f; 
 
     //WALL JUMP ONLY --------------------------------------------------
     public float jumpForce = 380.0f; 
     public float actualJumpForce = 0;
     public float wallJumpForce = 700.0f;
     public float actualWallJumpForce = 0;
     //-----------------------------------------------------------------
 
     public float airMove = 0; //Player horizontal movement while not grounded
     public float move = 0; //Player horizontal movement while grounded
     public bool grounded = false; 
     //Is the player Wall jumping from a wall on his right?
     public bool rightWallJump = false;
     //Is the player Wall jumping from a wall on his left?
     public bool leftWallJump = false;  
 
     //BUNCH OF CHECKERS MAINLY USED FOR WALL JUMP, WE'LL BE USING GROUNDCHECK ONLY ----------
     public Transform groundCheck;
     public Transform leftTopCheck;
     public Transform leftBottomCheck;
     public Transform leftCheck;
     public Transform rightTopCheck;
     public Transform rightBottomCheck;
     public Transform rightCheck;
     //---------------------------------------------------------------------------------------
 
     private float groundRadius = 0.1f; //ground checker radius
     private float sideRadius = 0.05f; //wall jump checkers radius
     public LayerMask whatIsGround;
 
     //USED FOR OTHER FUNCTIONALITIES
     public LayerMask whatIsRed;
     public LayerMask whatIsGrey;
     //------------------------------
     
     void Start()
     {
         if(PlayerPrefs.GetInt("LEVELSDONE") >= 10)
             wallJump = true;
     }
     
     void Update()
     {    
         //-------------------------------OTHER FUNCTIONNALITY, IGNORE----------------------------
         if(gameObject.GetComponent<ColorReactions>().redActive)
             whatIsGround = whatIsRed;
         else
             whatIsGround = whatIsGrey;
         //---------------------------------------------------------------------------------------
 
         if(grounded)
         {
             move = Input.GetAxis("Horizontal");
         
             if(Input.GetKey(KeyCode.Space) && !jumping)
             {
                 GetComponent<Rigidbody2D>().AddForce(new Vector2(0, baseJumpForce));
                 currentJumpForce += baseJumpForce;
                 jumping = true;
             }
         }
 
         //--------------------------------WALL JUMP ONLY, IGNORE----------------------------------
         else if(!grounded)
         {
             if(!rightWallJump && !leftWallJump)
                 airMove = Input.GetAxis("Horizontal");
             if(Physics2D.OverlapCircle(leftCheck.position, sideRadius, whatIsGround) 
                || Physics2D.OverlapCircle(leftBottomCheck.position, sideRadius, whatIsGround) 
                || Physics2D.OverlapCircle(leftTopCheck.position, sideRadius, whatIsGround))                
             {
                 if(wallJump)
                 {
                     if(Input.GetKeyDown("space"))
                     {
                         actualWallJumpForce += wallJumpForce;
                         actualJumpForce += jumpForce;
                         leftWallJump = true;
                         rightWallJump = false;
                         jumping = false;
                     }
                 }
                 else
                 {
                     if(!rightWallJump && !leftWallJump)
                         airMove = Input.GetAxis("Horizontal");
                 }
             }
             if(Physics2D.OverlapCircle(rightTopCheck.position, sideRadius, whatIsGround) 
                || Physics2D.OverlapCircle(rightBottomCheck.position, sideRadius, whatIsGround) 
                || Physics2D.OverlapCircle(rightCheck.position, sideRadius, whatIsGround))                
             {
                 if(wallJump)
                 {
                     if(Input.GetKeyDown("space"))
                     {
                         actualWallJumpForce = wallJumpForce;
                         actualJumpForce = jumpForce;
                         rightWallJump = true;
                         leftWallJump = false;
                         jumping = false;
                     }
                 }
 
                 else
                 {
                     if(!rightWallJump && !leftWallJump)
                         airMove = Input.GetAxis("Horizontal");
                 }
             }
         }
         //-----------------------------------------------------------------------------------------
     }
     void FixedUpdate()
     {
         //--------------------------------WALL JUMP ONLY, IGNORE----------------------------------
         if(actualJumpForce <= 0)
         {
             actualJumpForce = 0;
         }
         if(actualWallJumpForce <= 0)
         {
             rightWallJump = false;
             leftWallJump = false;
         }
         //----------------------------------------------------------------------------------------
         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);    
 
         if(grounded)
         {
             jumping = false;
             currentJumpForce = 0; //Force currently applied to the jump
             actualJumpForce = 0; //Vertical force applied to the wall jump
             actualWallJumpForce = 0; //Horizontal force applied to the wall jump
             rightWallJump = false; //Whether the player is walljumping from a wall on his right
             leftWallJump = false; ///Whether the player is walljumping from a wall on his left
             GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
         }
         //***************************************MAIN JUMP MANAGEMENT IS DONE HERE***********************************************
         else if(!grounded)
         {
             GetComponent<Rigidbody2D>().velocity = new Vector2(airMove * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
             if(jumping)
             {
                 if(currentJumpForce < maxJumpForce)
                 {
                     if(Input.GetKey("space"))
                     {
                         GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 10));
                         currentJumpForce += 10;
                     }
                 }
             }
         //***********************************************************************************************************************
             //--------------------------------WALL JUMP ONLY, IGNORE----------------------------------
             if(rightWallJump)
             {
                 airMove = 0;
                 if(actualWallJumpForce > 0)
                     actualWallJumpForce -= wallJumpForce/20;
                 if(actualJumpForce > 0)
                     actualJumpForce -= jumpForce/3;
                 if(GetComponent<Rigidbody2D>().velocity.y >= 0 && GetComponent<Rigidbody2D>().velocity.y < 5)
                     GetComponent<Rigidbody2D>().AddForce(new Vector2(-actualWallJumpForce, actualJumpForce));
                 else if(GetComponent<Rigidbody2D>().velocity.y < 0)
                     GetComponent<Rigidbody2D>().AddForce(new Vector2(-actualWallJumpForce, actualJumpForce - (GetComponent<Rigidbody2D>().velocity.y)));
                 else if(GetComponent<Rigidbody2D>().velocity.y >= 5)
                     GetComponent<Rigidbody2D>().AddForce(new Vector2(-actualWallJumpForce, 0));
             }
             if(leftWallJump)
             {
                 airMove = 0;
                 if(actualWallJumpForce > 0)
                     actualWallJumpForce -= wallJumpForce/20;
                 if(actualJumpForce > 0)
                     actualJumpForce -= jumpForce/3;
                 if(GetComponent<Rigidbody2D>().velocity.y >= 0 && GetComponent<Rigidbody2D>().velocity.y < 5)
                     GetComponent<Rigidbody2D>().AddForce(new Vector2(actualWallJumpForce, actualJumpForce));
                 else if(GetComponent<Rigidbody2D>().velocity.y <= 0)
                     GetComponent<Rigidbody2D>().AddForce(new Vector2(actualWallJumpForce, actualJumpForce - (GetComponent<Rigidbody2D>().velocity.y)));
                 else if(GetComponent<Rigidbody2D>().velocity.y >= 5)
                     GetComponent<Rigidbody2D>().AddForce(new Vector2(actualWallJumpForce, 0));
             }
             //-----------------------------------------------------------------------------------------
         }
     }
 }

Comment
Add comment · Show 2
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 drex150 · Mar 20, 2015 at 06:07 AM 0
Share

It would help a lot to see the script that controls jumping. Add that on here, be sure to use the code sample button.

avatar image saud_ahmed020 · Mar 20, 2015 at 12:49 PM 0
Share

Can you share your code ??

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D 360 degress platformer example needed 0 Answers

How can I create a "round arc" physics based jump? 2 Answers

The player doesn't jump to the left with the W key pressed 0 Answers

Grapple and swing/pull mechanic 2 Answers

The Mario Jump? 2 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