- Home /
How to check if a trajectory is blocked?
Hi, I currently have a working turret thats working fine, its able to turn and calculate the angle needed perfectly. However my problem now is I want it to only shoot if the trajectory of the bullet is clear and not blocked. I've tried raycasting but it doesn't account for the curve in the path due to gravity, which means sometimes the bullet is blocked but the raycast is clear and vice versa. I've taken some screenshots to illustrate:
The green line is a direct raycast to the target and the blue lines are raycasts off the direction of the barrel and turret base. So in the first picture the path is blocked but no raycasts are hit while in the second picture the raycasts are blocked but the path is clear. Basically I want the turret to stop shooting in picture 1 but continue in picture 2.
So how could I test if the trajectory of the bullet is blocked or not?
For some extra info heres the script which I'm using to fire the bullet. Its pretty standard where the angle and speed have both been calcuated, and the bullet is just a standard rigidbody.
var instantiatedProjectile = Instantiate(projectile,barrel.position,barrel.rotation);
instantiatedProjectile.velocity=barrel.TransformDirection(Vector3(0,0,speed));
Answer by senad · Jan 10, 2012 at 09:13 AM
The easiest way to implement this that comes to my mind would be to check for collisions at discrete points on the trajectory. For this you take steps along the trajectory and check each point. The performance might be ok, if you only have one or a few turrets.
I am sure that you can also come up with an equation for the trajectory and solve if it intersects with the other game objects. But you would have to come up with some advanced maths for it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062830im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Increase velocity without changing trajectory 1 Answer
Find trajectory angle needed to hit an object 0 Answers
Projectile trajectory 1 Answer