- Home /
Issues using physics for 2d platformer.
I have looked at many questions about the best way to control movement of a character in a 2d platform game and still can't find a decent answer. Also, none of them address the main problem I have which is that when I walk/run into a slope, not only does it let me go up very steep slopes, but it adds upwards force, and the character shoots up very far without jumping. This is the same if you just about make a platform when jumping to it and hit the edge.
I am using rigidbody2d.velocity = new Vector2 (move * maxSpeed, rb2d.velocity.y); for left and right movement, as suggested in a unity tutorial.
What is the best way to get round this, or, is there a better way to move the player?
Answer by 82MiddleMan · Oct 16, 2015 at 02:44 AM
Thanks for the support unity. I ended up using this tutorial with a few tweeks if anyone else is wondering https://www.youtube.com/playlist?list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz
Answer by GiyomuGames · Oct 16, 2015 at 02:44 AM
I don't have slopes on my game but I can understand why your character would become very fast: You are adding speed on the x axis regardless of the slope angle. Therefore it is trying to travel the same distance on the x axis as usual, but it needs to go faster as the actual distance is longer. The actual distance is "1 / cos(slope angle)" times the distance on a flat platform. From this formula you can see that if the slope angle is 0 then there is no change (cos(0) = 1), but the bigger the slope angle the greater the distance (and it explodes when the slope angle is 90 obviously as cos(90) = 0).
So you may want to do move * maxSpeed * Mathf.cos(slopeAngle)
when your character in on a slope (or all the time if you consider that a flat platform is a slope of 0 degree).
Answer by GiyomuGames · Oct 16, 2015 at 01:41 AM
You should multiply move * maxSpeed by the cosinus of your slope angle to prevent your character from going faster on the slopes.
Indeed without the cosinus your character is trying to travel the same horizontal distance regardless of the slope angle. Therefore the steeper the slope the faster it has to go.
Your answer
Follow this Question
Related Questions
Why doesn't Rigidbody.velocity.x = 0 in a collision? 1 Answer
2D Jump using "Rigidbody2D.AddForce" doesn't work. 1 Answer
How to check if my enemy hits the ground at a certain velocity then add explosive force. 1 Answer
Camera following Rigidbody jitter every few seconds with background 0 Answers
[2Dplatformer][Problem] When the player closes to the enemy, the enemy pass through the colliders 0 Answers