Test if a bullet will hit before shooting it?
Hello! I am making a tower defence like game. With enemys that can shoot the player. But. I want to make sure the bullet will hit before shooting. I am already raycasting. But. I am using addforce to add force to the bullet. So sometimes the ray will be casted. And it hits the object. So it shoots the bullet. But the bullet is too far away to hit the object so it falls down before hitting it. Here is how i am shooting the bullets: currentBullet.GetComponent().AddForce(transform.right * bulletSpeed);
Here is an example of what is going on:
The raycast hits the target (The line represents the raycast)
It looks like the bullet will hit. Because the ray hit the object. But since i'm just adding force. This happens
Shown from the picture above. The bullet doesn't hit the object. What can i do to fix this?
Answer by Cuttlas-U · Apr 09, 2017 at 05:22 PM
hi; Its not a simple job and to achive this u need to use Math equation;
I suggest u to watch these 2 videos to give u and idea about that :
https://www.youtube.com/watch?v=IvT8hjy6q4o&list=PLFt_AvWsXl0eMryeweK7gc9T04lJCIg_W∈dex=3
https://www.youtube.com/watch?v=phMZQNu0ZFM∈dex=2&list=PLFt_AvWsXl0eMryeweK7gc9T04lJCIg_W
Answer by OfficialCoatsee · Apr 09, 2017 at 07:35 AM
You could set the bullets trajectory to permanently target the target object and then clamp specific values such as the bullets Y value.
But if the target is moving, then the bullet would look like it's homing.
You could also get the distance between the gun and the target, and based on the distance, multiply the bullet speed by a certain speed attribute.
Maybe even also try getting the distance between the actual bullet and its target to keep it up to date.
I don't want to effect the bullet. I am just testing if the bullet will hit or not