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!
Your answer
Follow this Question
Related Questions
Need Help with Jump Coding 0 Answers
My ground checker only works sometimes 0 Answers
Collision2D or Trigger2D - Jumping Issue 0 Answers
Jumping on enemies help 0 Answers