- Home /
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));
}
//-----------------------------------------------------------------------------------------
}
}
}
It would help a lot to see the script that controls jumping. Add that on here, be sure to use the code sample button.
Your answer
Follow this Question
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