- Home /
Dashing through enemies, but not walls.
So I am new to unity and have been trying to create a 2d platformer. I recently put in a dash mechanic. The dash works by adjusting the player's transform.position. I did it that way because I wanted it to be more like a teleport rather than a dash and wanted them to be able to travel through enemies. However I soon found that this also made them able to pass through walls as well. I've tried a raycast with a layer mask to ignore enemies, but it isn't working for some reason. Its not even detecting the enemies. Is there another way I can do this without a raycast, or any reason the raycast isn't working?
Answer by kylecat6330 · Jul 04, 2020 at 05:50 PM
If you are raycasting from your player your raycast is most likely hitting the player's collider. Add a Debug.Log() to your script which will tell you what the Raycast his picking up. If its hitting the player's collider add it into the layer mask of the raycast to ignore.
You should also consider using a Debug.DrawRay to see where your raycast is as you may have just misplaced it.
Hope this helps.
Looks like it was definitely picking up the player. I haven't tested ignoring it in the layer mask thing yet, but I'm sure that'll do the trick now that I know what the problem is. Thanks!
A quick question, so I managed to get what you said to do working, but the player will sometimes teleport partially into the wall. How do I fix this?
@FriskyFrisk77 Keep in $$anonymous$$d that transform.position is the center of the transform. You will need to offset whatever adjustments your making to the position so that the entirety of the object is out of the way.