Simple Cube Side Collision Detection for 2D Platformer

I have a box collider with a rigidbody for a 2D platformer. The player moves left and right on the X-axis using physics. My player moves left and right continuously as needed and as a result will go through walls since logically the player’s character will add or subtract 1 from the X-axis to continuously move. I saw online that I can use Raycasts to detect side collisions, but I have been told that Raycast use up a lot of resources. Since I am designing/developing for mobile I’m trying to use as little as possible resources. I also know there are ways to detect Collision.contacts as seen in the Unity - Scripting page but it’s hard to find a way to write in script form for the player character to look for when a side is hit. Need examples that are more specific to which side to look at is being hit, if that makes sense.

I am looking for advice and clarification on the Raycast resource usage and if there is a more effective way to detect side collisions.

I only understand Javascript at the moment, and have been trying to figure this out for 3 weeks.

Hope to rewrite everything in 2D later on if the 2D physics use up less resources, so any experience on this will be great.|

If anyone can give me an example to try out please post. Thank you.

Rather than ask, test. With Unity I find that things I’m sure would be true are not. And there is a lot of misinformation out there. Only testing on the target platform gives me the accurate information. Even long-standing beliefs founded on good CSci sometimes fall by the wayside. So find some sort of FPS script:

And write some code that does a raycast repeatedly in a loop. Run it on the target platform, and ncrease the number of times the ray is cast until you get an FPS drop. This will give you a starting point for costing raycasts. I think you will be (pleasantly) surprised).