I am working on a infinite runner game for android using Unity. The player runs (automatically) and has to avoid the obstacles by touching the screen and changing the gravity. But i sometimes when he hits the corner/side of the platform, the player gets stuck. I already applied a “noFriction” material on the player but it still happens sometimes. (The small boxcollider under the player just checks if the player is touching the ground)
This will either be because of the colliders you’re not showing on the platform or your movement control code. Friction won’t keep it attached like that unless your gravity is extremely low.
The whole point of the Rigidbody2D is to control the Transform and move via its velocity yet you are explicitly overriding physics and modifying the Transform yourself per-frame, even though physics isn’t running per-frame.
Do not modify the Transform when using physics. Use the Rigidbody2D API for movement.
Ohh okay, sorry i am new to unity and i followed a couple of tutorials for the movement. I tried it with velocity but i think i am doing it wrong. I made a new method which sets rb.velocity = movement * movespeed. But it doesnt seem to accelerate and the gravity is very weak.
Why would it accellerate, you’re setting the velocity to a fixed value which is a set speed in a certain direction. Also, gravity works by changing the velocity which again is being stomped over by you setting it explicitly.
If you want acceleration then add forces using AddForce. It might be worth looking at a few tutorials on movement using physics such as here.
As a side note, 2D physics uses 2D vectors so using 3D vectors is kind of redundant.
Okay it moves properly now using Velocity but it still gets stuck at the corner of platforms so i guess that was not the issue why it got stuck there in the first place?
If you’re still setting velocity directly then physics obviously cannot change the velocity (you stomp over it) so it can fall correctly or correctly move out of overlaps such as you have with your platform.
I am sorry can u tell me what u mean with setting it directly? You mean setting it in the Fixed Update or do you mean the CheckSpeed Method because i deactiavted that for the test.
The physics system modifies the velocity due to gravity, collisions (etc) and you’re stomping over it with your value. This is why you use forces because they add to the existing velocity, not overwrite it.
I would suggest looking at the tutorial link I provided above.