I am currently developping a top down dungeon crawler and settled on a kinematic rigidbody for the main player since i dont plan on adding any physics.
I am using raycast based detection to check if i’ve hit a wall but it seems that the collisions are not always occuring at the same time.
Here’s a gif to illustrate what i mean.
consideratemaleaxolotl
Here you can see that sometimes the cube stops far away from the wall, and sometimes closer or even touching it.
Im a bit confused as to how to improve the ray’s detection. Should i increase physics iteration? Should i cast a longer ray? Any help would be greatly appreciatted.
You’re getting inconsistencies because you’re only zeroing out your velocity when you encounter a wall, rather than accounting for the distance to the wall. Instead of raycasting a fixed amount, you should be raycasting the distance that you are expecting to travel in the current frame. If you hit something, truncate the travel distance to be the distance to the wall. This will ensure that you always end up perfectly flush.
For more details, Sebastian Lague has an older tutorial series that illustrates this technique nicely.
@Putcho ; My rigidbody is kinematic, it doesnt have gravity and yes it is being moved by input using “MovePosition” on the rigidbody.
@Madgvox ; Setting the raycast length to be equal to the distance im expecting to travel seems like it would fix the issue and remove the potential misses coming from the frame updates. I’ll try to change it up so that the ray’s length depends on my speed and will keep you updated.
Thanks to your input i was able to get a consistent collision point and got the player to stop precisely on the same point on every collision no matter the speed at which the collision happens.