Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by william9518 · Oct 06, 2013 at 04:24 PM · bulletgunshootdrop

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 06, 2013 at 05:14 PM 0
Share

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;
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image william9518 · Oct 06, 2013 at 08:12 PM 0
Share

Thats... Performance expensive. I think I will stick with meat5000's answer.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 06, 2013 at 05:14 PM 0
Share

@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.

avatar image william9518 · Oct 06, 2013 at 08:02 PM 0
Share

Alright I'll try it. Thanks. Will be back with more feedback later.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges