- Home /
Bullet Drop With Raycast
Before you start scolding me, I have checked many different search engines about this, including Unity Answers. I am attempting to make a realistic post-apocalyptic game, and my bullets need some bullet drop. I made my shooting script like so:
if(ItemType == ItemTypeEnum.GunFullAuto){
if(Input.GetButton("Fire1")){
if(AmmoInCurrentMag > 0){
FireOneShot();
ReloadAnim.animation.CrossFade(itemName + "Shoot", 0.1);
recoil2 -= new Vector3(0, 0, recoil / 2);
recoil4 -= new Vector3(recoil * 75, Random.Range(recoil * 100, -recoil * 100), 0);
recoilNeck += new Vector3(-recoil * 30, Random.Range(-recoil * 30, recoil * 30), 0);
fireTimer = 5;
} else {
ReloadAnim.animation.CrossFade(itemName + "Idle", 0.1);
}
} else {
ReloadAnim.animation.CrossFade(itemName + "Idle", 0.1);
}
}
You can ignore the FireTimer stuff, as that is irrelevant here. My bullet is a instantiated empty GO which will activate a Raycast, like so:
function Start(){
if(networkView.isMine)
networkView.RPC("SetOwner", RPCMode.All, Database.user);
var Hit : RaycastHit;
var temp : Vector3 = gameObject.transform.position + transform.forward * maxDistance;
Tracer.GetComponent(LineRenderer).SetPosition(0, gameObject.transform.position);
if(Physics.Raycast(transform.position, transform.forward, Hit, maxDistance)){
if(!Hit.collider.isTrigger){
if(HitMetal){
var myMetal = Instantiate(HitMetal, Hit.point + (Hit.normal * HitParticleSpacing), Quaternion.LookRotation(Hit.normal));
myMetal.transform.Rotate(90, 0, 0);
myMetal.transform.parent = Hit.collider.gameObject.transform;
}
if(Hit.collider.gameObject.tag == "Player"){
Hit.collider.gameObject.GetComponent(PlayerHealthScript).Blood -= damage;
Hit.collider.gameObject.GetComponent(PlayerHealthScript).BleedingLevel += damageBleed;
Hit.collider.gameObject.GetComponent(PlayerHealthScript).SetLastShotBy(owner);
}
Tracer.GetComponent(LineRenderer).SetPosition(1, Hit.point);
} else {
Tracer.GetComponent(LineRenderer).SetPosition(1, temp);
}
} else {
Tracer.GetComponent(LineRenderer).SetPosition(1, temp);
}
}
Anyone have any idea how I can make a bullet drop OTHER THAN ROTATING THE BULLET? I should not rotate the bullet as in multiplayer the tracer would look diagonal...
Answer by robertbu · Oct 06, 2013 at 04:59 PM
As an approximation, you can add a slight downward rotation to the firing vector. This will cause the bullet to drop more the further the target is from the gun.
var firingVector = transform.forward;
var axis = Vector3.Cross(firingVector, Vector3.down);
firingVector = Quaternion.AngleAxis(smallAngle, axis) * firingVector;
You may have to reverse the parameters in the Vector3.Cross, or alternately change the sign of 'smallAngle'. You use firingVector in place of transformForward in the Raycast(). This will work well as long as the character is aiming somewhat horizontally. If you are allowing your character to shoot nearly straight up or down, then you'd need something a bit more complex.
Alternate solution that handles shooting straight up or down. It is still an approximation since a bullet's fall is not linear. Figure out or assign the fall at some distance. Say we use 100 and it falls 0.45 at that distance. Then you could:
var firingVector = transform.forward;
firingVector = firingVector * 100 + Vector3.down * 0.45;
Answer by crazysurge · Oct 06, 2013 at 04:56 PM
Like bullet drop gravity? like in bf3? Then do this have the gmae make multiple frames of the linerenderer and render a new ray to the drop just delete render delete render, down and down, other then that sorry
Thats... Performance expensive. I think I will stick with meat5000's answer.
Answer by meat5000 · Oct 06, 2013 at 04:56 PM
Search google for trajectory maths.
Using the distance returned from the raycast, decide your own figures for speed and mass and use trajectory maths to work out your bullet drop at the given distance.
For the sake of saving fps it may be worth reusing the value worked out in the first bullet calculations for the next bullets if the distance returned is within 1 unit, for example.
Basically you work out the distance a projectile falls in the time it takes for the projectile to reach the target at a given velocity. The two axes can be handles separately.
@meat5000's answers is the more accurate in terms of bullet fall. If you need a really accurate solution, this is the one. You need to use the angle of the shot and get both the vertical as well as horizontal velocity in calculating the the fall. But it has one issue you need to be aware of. In order to get the distance, you have to use a Raycast. But what the Raycast hits is not necessarily what the bullet would hit. As a first approximation, you'd need to do a new Raycast after the angle is calculated.
Alright I'll try it. Thanks. Will be back with more feedback later.
Answer by Firedan1176 · Nov 16, 2014 at 03:26 AM
You could also make your bullets use rigidbodies. 90% of the time it will just pass through whatever you shoot and keep going on. That's why you should shoot a raycast from your bullet out 5 meters or so. If anything touches it, it's as good enough of a hit as any. Alternatively, you can shoot the raycast backwards.
Your answer
Follow this Question
Related Questions
How to make a gun shoot? 2 Answers
Shooting. Bullet floats and sprays 1 Answer
Troubles With A Shoot Script 1 Answer
how to make bullets apply damage 1 Answer
How do i make a fps where the gun always shoots realisticly 3 Answers