- Home /
how do I use a Raycast to apply force to the player on collision (in 2d)?
Hi there! I'd like to thank you for taking the time to read (and hopefully reply ;) ) to my question. So, here's the setup: I have two players that can already run around and jump with ground detection. I want to make a bumping effect when the players collide. I tried using a bouncy physics material 2d, but had two issues with this. 1: The players began bouncing on the ground. This doesn't seem like that much of a problem, but what ended up happening is it was really hard to time a jump so that the detection would let you. 2: The players didn't really bump each other around. Instead, they just started doing this really weird jittery thing. (collision detection on continuous or discrete, didn't matter) This was not at all what I was hoping for. So here's what I've come up with.
Step 1: When the players collide, detect that, and create a point on the player where the collision happened.
Step 2: Create a raycast coming out of the point (with some randomization of the angle), in the opposite direction that the player was touched, and possibly temporarily disable player movement if you are able to cancel it by holding down the button to move in the opposite direction.
Step 3: Add a small, varying amount of force in the direction of the raycast.
that's it. pretty simple.
Unfortunately though, I've not been able to make it work. I don't have any code to share as I've scrapped what I had already.
If anyone knows how to do something to this effect, I would really appreciate it if you could help me out.
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613014530im_/https://answers.unity.com/themes/thub/images/avi.jpg)