Why does my player bounce when hitting edge of platform
I have a character object who can move back and forth and jump. The problem comes from the player bouncing a bit sometimes when it hits at the ends of a platform. This leads to an anoying ocurrance where the player will sometimes hit the plaform, but then bounce back of the platform withouth meaning it, or the player trying to jump again when hitting the ground, but instead just not reacting. The player is not supposed to be able to jump while in the air, but it should be able to jump immediately after landing. The player is supposed to work more like it does when landing on the middle of a platform where it just lands straight away and can jup immediately. My guess would be that the error is somewhere to be found in the code I have added. Hope someone is able to help me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhysicsObject : MonoBehaviour
{
public float gravityModifyer = 1f;
public float minGroundNormalY = .65f;
protected Vector2 targetVelocity;
protected Vector2 velocity;
protected Rigidbody2D rb2d;
protected ContactFilter2D ContactFilter;
protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
protected List<RaycastHit2D> hitBufferList = new List<RaycastHit2D>(16);
protected bool grounded;
protected Vector2 groundNormal;
protected const float minMoveDistance = 0.001f;
protected const float shellRadius = 0.01f;
// Start is called before the first frame update
void Start()
{
ContactFilter.useTriggers = false;
ContactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
ContactFilter.useLayerMask = true;
groundNormal = new Vector2(0f, 1f);
}
// Update is called once per frame
void Update()
{
targetVelocity = Vector2.zero;
ComputeVelocity();
}
protected virtual void ComputeVelocity()
{
}
void OnEnable()
{
rb2d = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
velocity += gravityModifyer * Physics2D.gravity * Time.deltaTime;
velocity.x = targetVelocity.x;
grounded = false;
Vector2 deltaPosition = velocity * Time.deltaTime;
Vector2 moveAlongGround = new Vector2(groundNormal.y, -groundNormal.x);
Vector2 move = moveAlongGround * deltaPosition.x;
Movement(move, false);
move = Vector2.up * deltaPosition.y;
Movement(move, true);
}
void Movement(Vector2 move, bool yMovement)
{
float distance = move.magnitude;
if (distance > minMoveDistance)
{
int count = rb2d.Cast(move, ContactFilter, hitBuffer, distance + shellRadius);
hitBufferList.Clear();
for(int i = 0; i < count; i++)
{
hitBufferList.Add (hitBuffer[i]);
}
for (int i = 0; i < hitBufferList.Count; i++)
{
Vector2 currentNormal = hitBufferList[i].normal;
if (currentNormal.y > minGroundNormalY)
{
grounded = true;
if (yMovement)
{
currentNormal = groundNormal;
currentNormal.x = 0;
}
}
float projection = Vector2.Dot(velocity, currentNormal);
if (projection < 0)
{
velocity = velocity - projection * currentNormal;
}
float modifiedDistance = hitBufferList[i].distance - shellRadius;
distance = modifiedDistance < distance ? modifiedDistance : distance;
}
}
rb2d.position = rb2d.position + move.normalized * distance;
}
}
Your answer
Follow this Question
Related Questions
Player bounce when not supposed to 1 Answer
How can you make a level fly in and fall out as the player progresses? 0 Answers
[Help] Player stops moving when hitting wall diagonally 0 Answers
any tutorial for playercontroll, physics ? 0 Answers
When friction on physic material 2d is at 0 my player slide 0 Answers