Unity 2D : how can I keep my character within the left n right walls and let the objects come from both the sides( big rocks), something like this in the picture below and also they should just only collide with the character and should pass through each other something like ignoreCollisionForce() or…
However you’re handling the player input to move the character, simply get the screen width and confine the player.x within the width. For example, once you have the minimum and maximum horizontal positions the player can move between (ie. screen width in units, or predetermined values like -6 and 6), use Mathf.Clamp() on the player.x in Update().
As for the collisions with the rocks, do you already have them colliding? Your question is a little unclear here. If you do, then clamping the player horizontal position won’t effect the rocks at all.
If you don’t, then in your OnCollisionEnter2D (or OnColliderEnter2D, OnTriggerEnter2D, etc.) just check the objects tag before you do anything. For example, if it’s colliding with an object with the tag “Rock”, don’t do anything. If it’s the tag, “Player”, do something. There are plenty of tutorials on this.
Some keywords to look up would be: 2d collision, OnTriggerEnter2d, comparing tags, etc.
Good luck @KimMingyuSVT