- Home /
Raycasting in 2D to prevent a player from moving through walls?
Hi all, Unity noob here,
I am making a 2D top-down game the player moves with grid-based movement (i.e. Pokemon). Now I know that collisions with grid-based movement are tricky, and since I can't wrap my head around how to do this with a 2D array, I've decided I want to do it with raycasting. Here's my scene:
I would like to make it so a ray is being "shot out" from my player in all four directions, and if the ray's distance (between player and wall) is less than 1 tile, then the player will be prevented from moving in that direction. I've had a really hard time finding Raycast2D tutorials that I can wrap my head around. How do I best set up these 4 rays and how do I then check to see if they're colliding with something?
Answer by Cuttlas-U · Sep 09, 2017 at 07:33 PM
hi; I once saw a sample code and plugin for this ;
but a better idea :
u can easily create a BoxCollider2D and make it like a ray ( reduce Y size value );
then all the things u need is just a TriggerEnter check to set a Bool like Blocked = true;
Your answer
Follow this Question
Related Questions
Rigidbody 3D Sticking issue 2 Answers
Need help with raycast movement. 1 Answer
CircleCast hits emitter with Collider2D 0 Answers
Vector3.Movetoward. Why does this work? 0 Answers
2D Raycast effect only what it hits? 2 Answers