- Home /
AI reflect shooting
Hello there! I'm making a 2D action-platformer game with reflect shooting mechanics. The main problem is how to make AI predict bullet trajectory to catch a player.
So is there any best way to find these blue sides of triangle? Well i had an idea to make a tons of Raycasts but i dont think this is the best way
You can use Unity's built in method Vector3.Reflect()
and Raycasts... I think this is the best way to do it
There is no problem to reflect a vector. Bullets are reflecting. But the problem is how to find this direction. The only way i figured out is to cast thousands of raycasts and check all of them, but i dont think it's optimized enough
Could you send a picture of your script too? Would be helpful in analyzing the issue and wat you are currently doing...
will try to develop into a proper answer and a demo, but you can calculate the reflection point w/o too much trouble. as so often, similar triangles are your friends.
Well i got a genios solution on stackowerflow. The idea is to represent the reflecting ray as a mirrored space in one direction.
Its sounds complicated but tt's actualy not. The only thing you need to do is to build a trajectory from a mirrored space to original character. If a ray from mirrored space goest to a player position without any collisions (exept outer walls) you can get the wall collision point to choose it's as a direction. If it's collides with inner walls - you can reflect it in a mirrored space, and if it's hit the mirrored player position - this also a right direction to shoot. So basicaly the only thing you need to do is to cast 3 rays from 3 mirrored spaces and check them. That all! Hope it's will help someone
For better understanding: center room is the original room
black and green rays is the expected direction to player position
right left and down rooms are mirrored spaces
blue dotted lines are mirrored caster directions to original player
green dotted lines are mirrored space reflected directions