im making a 2d platformer game and currently, when the player sits on a slope, they face the direction the slope is. this means they are tilted. however, when you jump, it looks very awkward to jump at a 45 degree angle so i added this line of code
the problem with this is that it is very sudden so now i am wondering, how can i get the player to rotate to quaternoin.identity smoother and not have an instant transition?
also the problem with this is that if the slope is too steep, it wont register as being on the ground so it will rotate to quaternion.identity but then, the groundcheck will think that it is grounded and thus will be able to walk up slopes that they should not be able to. how can i ffix this?
You can use the static lerp methods of Quaternion like Slerp or Lerp.
float lerpWeight = 0.1; // Any value between 0 and 1, 0 being slow and 1 being sudden. Time.deltaTime should also work.
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, lerpWeight);
var hit = Physics2D.CircleCast(origin, radius, direction); // Note:
// In my experience the "direction" argument
// is not really important so you can leave that as Vector2.zero
float dot = Vector2.Dot(hit.normal, Vector2.up);
float absDot = Mathf.Abs(dot); // Get the abs since dot product is -1 to 1
const float limit = 0.5f;
if (absDot > limit)
{
// Stop or slow down the player
}
var hit = Physics2D.CircleCast(groundCheckPos.position, groundRadius, Vector2.zero);
// Note:
// In my experience the "direction" argument
// is not really important so you can leave that as Vector2.zero
float dot = Vector2.Dot(hit.normal, Vector2.up);
float absDot = Mathf.Abs(dot); // Get the abs since dot product is -1 to 1
const float limit = 0.5f;
if (absDot > limit)
{
// Stop or slow down the player
canWalk = false;
}
else
{
canWalk = true;
}
private void FixedUpdate()
{
moveInput = Input.GetAxis("Horizontal");
if (canWalk)
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
else
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y);
}
This is an interesting question. However, practically speaking, the vast majority of games do not tilt players which are in contact with a slope! It is far more natural to just fix the player’s rotation. If you think to your favourite 2D platformers, chances are that the characters walk on slopes as if it were flat ground. At most they might slow you down, or make you slip down due to gravity.
You probably don’t need to freeze it, just remove the logic where you rotate the player on a slope. I know it probably feels bad to just remove something you’ve worked on for a long time, but ultimately this is a design problem.
i dont have any code to rotate it xDDD its just a side affect from my script, i change the rb velocity and it just happens to rotate it onto the slope lol