- Home /
[2D] How to stop my character on slopes?
I've got issue with slopes and chaarcter. When I'm going in a high slope, my character goes up (exactly this) and I don't want it. When program prints "On high slope" I want to make fall character down and be unable to walk on slopes, but I don't know how to do this. Maybe I'm approaching it wrong. Thanks in advance.
//I just have high slope detection
//I put it in FixedUpdate()
private void Slope() {
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 3f, whatIsGround);
if (hit.collider != null) {
if (Mathf.Abs(hit.normal.x) > maxSlope) {
print("On high slope");
}
}
}
Answer by RaphaelQuem · Sep 10, 2018 at 04:23 PM
Are you using a RigidBody or only a BoxCollider? And what code are u using to move it around?
I'm using Rigidbody2D with BoxCollider2D and this is my entire movement code https://pastebin.com/mw$$anonymous$$mijak
Okay!
So, i'm not with my unity available right now, but if i understood your code right, if you add "if($$anonymous$$athf.Abs(hit.normal.x) <= maxSlope)" wrapping the line 71 and 72, it'll do.
It won't add any force to the rigidbody programatically and the unity gravity will take care of the body sliding down the slope.
If the player continues to push forward, it will remain in the max slope, since when it reaches the top it slides back a little.
Okay, That works fine! I have some little issues with this solution (e.g. I can't move in the direction opposite to the slope), but I think I can solve that problem :D. Thank you so much <3
Your answer
Follow this Question
Related Questions
How to keep a 2D platformer character grounded when traversing slopes. 0 Answers
Is it possible to swap my x Axis and y Axis on a rigidbody2D? 1 Answer
Change Speed of Multiple Objects of the same Class 2 Answers
Physics makes 2D object rotate around its pivot 1 Answer
My object is rotating crazily - 2D 1 Answer