Question by
pf_wang · Aug 23, 2020 at 05:28 AM ·
ground detection
Ground check and changing gravityScale
I am making a Side-scrolling 2d game and would like to make the player moving on the slope without sliding.
I tried to realize this by setting the Rigidbody2D.gravityScale to 0 when the player touches the ground and 1 when the player leaves the ground.
void OnTriggerEnter2D(Collider2D Coll)
{
if (Coll.gameObject.tag == "Ground")
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.gravityScale = 0;
}
}
void onTriggerExit2D(Collider2D Coll)
{
if (Coll.gameObject.tag == "Ground")
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.gravityScale = 1;
}
}
The default gravityScale is 1. The program successfully changes the gravityScale to 0 while touching the ground but never changes back to 1 when the player leaves the ground.
Is there any problem with my code? Or is there any better solution?
Comment
Your answer
Follow this Question
Related Questions
Rootmotion on slopes issue 1 Answer
Player sometimes doesnt jump Instantly when clicked 1 Answer
Question about ground detection 2 Answers