- Home /
[2D] How can I fix player's slope moving problem?
Hi, I'm creating megaman-like 2D platformer game. I cannot solve how to fix my slope problem.
This image and movie imply my problem.
https://www.youtube.com/watch?v=2dVgjvniJ3Q&feature=youtu.be
I think, first situation is occurred because there's y direction inertia. so when I stop my player but y velocity still remains and it results my character to fall in a short moment.
second situation is, when player moves to down, it floats a moment. That makes my character shows fall animation.
So I concludes that, both situations are occurred because my character is not attached to ground completely...
I want to make my project just like Megaman X series. That means, Player must be attached to slope when it is moving.
Gravity is 0, ground checking uses EdgeCollider2D and set layer as 'ground'
You can download my test project in here. https://drive.google.com/file/d/0B_piXOzIlhTsX3R2ajdpTnllb0U/view?usp=sharing
This is my player controller source.
using UnityEngine;
using System.Collections;
/// <summary>
/// Player Controller
/// </summary>
public class PlayerController : MonoBehaviour
{
#region Components list
BoxCollider2D _collider;
Rigidbody2D _rigidbody;
Animator _animator;
#endregion
#region public fields Unity can accessible
public float movingSpeed = 5;
public LayerMask whatIsGround;
public LayerMask whatIsWall;
#endregion
#region fields and property implies player's physical state
bool landed = false;
bool facingRight = true;
bool moving;
/// <summary>
/// Is player grounded?
/// </summary>
bool Landed
{
get { return landed; }
set { _animator.SetBool("Landed", landed = value); }
}
/// <summary>
/// Is player moving?
/// </summary>
bool Moving
{
get { return moving; }
set { _animator.SetBool("Moving", moving = value); }
}
#endregion
#region overrides MonoBehaviour default methods
void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
_animator = GetComponent<Animator>();
_collider = GetComponent<BoxCollider2D>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.X))
{
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x, 16);
}
}
void FixedUpdate()
{
// check if player touches ground
Landed = _collider.IsTouchingLayers(whatIsGround);
if (Landed)
{
}
else
{
// fall player if it is not on the ground
float vy = _rigidbody.velocity.y - 0.8f;
if (vy > 16) vy = 16;
_rigidbody.velocity = new Vector2
(_rigidbody.velocity.x, vy);
}
// handle keyboard input
if (Input.GetKey(KeyCode.LeftArrow))
{
MoveLeft();
}
else if (Input.GetKey(KeyCode.RightArrow))
{
MoveRight();
}
else
{
StopMoving();
}
}
#endregion
#region assist methods
/// <summary>
/// move player to left
/// </summary>
void MoveLeft()
{
if (facingRight)
Flip();
_rigidbody.velocity = new Vector2(-movingSpeed, _rigidbody.velocity.y);
Moving = true;
}
/// <summary>
/// move player to right
/// </summary>
void MoveRight()
{
if (facingRight == false)
Flip();
_rigidbody.velocity = new Vector2(movingSpeed, _rigidbody.velocity.y);
Moving = true;
}
/// <summary>
/// stop player's moving
/// </summary>
void StopMoving()
{
_rigidbody.velocity = new Vector2(0, _rigidbody.velocity.y);
Moving = false;
}
/// <summary>
/// flip player's direction
/// </summary>
void Flip()
{
facingRight = !facingRight;
Vector3 newScale = transform.localScale;
newScale.x *= -1;
transform.localScale = newScale;
}
#endregion
}
Sorry for my poor English, but thanks for reading.
Your answer
![](https://koobas.hobune.stream/wayback/20220612001222im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem sliding down hills (WITH VIDEO) 1 Answer
Travel up and down slopes 0 Answers
Slope angle fix 0 Answers
How to rotate the Rigidbody based on the normal of the ground?? 1 Answer
Collision normal angles. 1 Answer