- Home /
How to Handle Corners on a Slope in a 2D Platformer Game?
So I've been following a unity live training session and integrated the code that they wrote into my 2D platformer game. After doing so, I came across a slight problem and it goes like this. I have a 1D blend tree in my animator controller that plays animations(jumping and falling animations) based on the vertical speed of the character. What happens when the character hits the edge of a slope is that it changes the vertical speed of character and plays one of the falling animations in the blend tree instead of the player just moving up or down the slope. Here is an image of the player executing its jumping animation as it touches the start of the slope.
And here is the relevant code from the training session.
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class BaseEntityController : MonoBehaviour
{
[SerializeField]private float gravityModifier = 2f;
[SerializeField]private float minGroundNormalY = 0.65f;
protected Vector2 velocity;
protected Vector2 targetVelocity;
protected Rigidbody2D rb2D;
protected ContactFilter2D contactFilter;
protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
protected List<RaycastHit2D> hitBufferList = new List<RaycastHit2D>(16);
protected const float minMoveDistance = 0.001f;
protected const float shellRadius = 0.01f;
protected bool grounded;
protected Vector2 groundNormal;
private void OnEnable()
{
rb2D = GetComponent<Rigidbody2D>();
}
private void Start()
{
contactFilter.useTriggers = false;
contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
contactFilter.useLayerMask = true;
}
private void Update()
{
targetVelocity = Vector2.zero;
ComputeVelocity();
}
protected virtual void ComputeVelocity()
{
}
private void FixedUpdate()
{
velocity += gravityModifier * Physics2D.gravity * Time.fixedDeltaTime;
velocity.x = targetVelocity.x;
grounded = false;
Vector2 deltaPosition = velocity * Time.fixedDeltaTime;
Vector2 horizontalMovement = new Vector2(groundNormal.y, -groundNormal.x);
Vector2 move = horizontalMovement * deltaPosition.x;
Movement(move, false);
move = Vector2.up * deltaPosition.y;
Movement(move, true);
}
private void Movement(Vector2 move, bool yMovement)
{
float distance = move.magnitude;
// This if statement checks to see if it is possible for us to collide with something in the next frame.
if(distance > minMoveDistance)
{
int totalColliders = rb2D.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
hitBufferList.Clear();
for (int i = 0; i < totalColliders; 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)
{
groundNormal = currentNormal;
currentNormal.x = 0;
}
}
float projection = Vector2.Dot(velocity, currentNormal);
if(projection < 0.0f)
{
velocity = velocity - projection * currentNormal;
}
float modifiedDistance = hitBufferList[i].distance - shellRadius;
distance = modifiedDistance < distance ? modifiedDistance : distance;
}
}
rb2D.position = rb2D.position + move.normalized * distance;
}
}
So from what I can understand, this section of code
float projection = Vector2.Dot(velocity, currentNormal);
if(projection < 0.0f)
{
velocity = velocity - projection * currentNormal;
}
is what slows down the velocity vector only when the projection value obtained from the dot product of the velocity vector and the normal vector that the player collides with is negative(in other words both vectors are opposites each other such as when a player hits a wall or a ceiling). I've tried modifying the vertical speed threshold in the blend tree but that didn't help. How can I make it so that the player doesn't start playing one of it's falling animations the moment it starts running up and down the slope?
Your answer
Follow this Question
Related Questions
Jagged white edges around partly transparent textures in GUI 2 Answers
Camera following Rigidbody jitter every few seconds with background 0 Answers
Bumper physics not working, 1 Answer
How should I rotate my character on a slope? (2d game) 0 Answers
While Moving Left or Right my character falls more slowly. 2 Answers