- Home /
Will I need to use raycasts for my 2D game?
In order to answer this best, I need to explain some mechanics that I want in my game:
I plan on making a platform fighter in which each player has the ability to "cling" to walls and jump off them. They can also cling to angled walls and ceilings, being able to jump off those surfaces too.
I plan on making the angle at which a wall is "cling-able" to be above 75 degrees. That means that if a wall has an angle of greater than 75 degrees, or less than -75 degrees, a player can cling onto the wall.
Considering these mechanics, I was wondering if I'd need to use raycasting to not only calculate the angles at which a player can cling onto a wall, but also calculate the angles the player would jump off a wall based on the wall's angle.
If I don't have to use them, that'd be great since I don't really know how to use raycasts. If I do have to use raycasts based on the mechanics I want for my game, I'd be willing to learn about them, but I'm not sure where I should start to learn about them. I read the Unity documentation, but I don't have a good idea on how to use raycasts practically. I know I'd need to calculate the "noraml" of the raycast to get the angle of a wall, but that's about it.
Also, something that worries me about raycasting is whether or not it'll interfere with collision boxes. Can I still use colliders when using raycasting? Having different colliders for each player will be important since the game is also a fighter, and I'd need to be able to detect when a player's hurtbox collides with another player's hitbox, when two hitboxes collide, etc.
Answer by Serinx · Apr 19, 2018 at 02:06 AM
Learning about raycasting is extremely useful in game development. Whether you use it for this problem or some other problem, I'm sure you will use them at some point.
They are fairly simple to use once you understand them. Try watching a few tutorial videos to get the hang of them.
You can use something called a LayerMask which allows you to ignore certain objects when raycasting.
E.g. if you wanted to check the wall angle, but didn't care if another player was in the way you could pass in the layermask for the wall and the raycast will continue through the player and return the hit on the wall.
I highly recommend learning about raycasts, don't be afraid to jump in and learn these things. Unity has made a lot of these functions extremely user friendly and they're there to help you, not hinder you.
Yeah I had a feeling it'd be a really useful thing to learn. I'll go ahead and $$anonymous$$ch myself about it. And I'll look into layermasks too. They may be what I'm looking for when considering hurtboxes and hitboxes.