Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Simpowitch · Sep 20, 2019 at 11:56 AM · physicsvector3mathbullettrajectory

Calculating 3D Physics Prediction of Shot Direction with Moving target and moving gun (inertia)

Hi. I need to predict the target position of a moving object in a 3d world for a gun to fire from a distance and hit the moving object.

So far it works perfect when the player is not moving. However when the player starts moving things become tricky. The bullets speed is not constant. It depends on the player's vector3 in comparison to the bullets trajectory. For instance shooting straight forward adds the player's speed to the bulletspeed and vice versa.

My calculation is currently dependent on a constant bullet-speed, which is problematic since the player's ship affects the bullets speed. If anyone has any tips I would be very happy! (google has helped me so far, but with the "inertia" problem (shipspeed+= bulletspeed), I'm lost).

This is my code so far:

 private Vector3 GetDirectionToShoot(Vector3 targetPos, Vector3 targetVelocity)
         {
             Vector3 totarget = targetPos - Ship.Position;
 
             targetVelocity -= Ship.LinearVelocity;
 
             float bulletSpeed = Ship.GunInfo.ProjectileSpeed;
             //bulletSpeed -= Vector3.Distance(Ship.LinearVelocity, targetVelocity);
 
 
             float a = Vector3.Dot(targetVelocity, targetVelocity) - (bulletSpeed * bulletSpeed);
             float b = 2 * Vector3.Dot(targetVelocity, totarget);
             float c = Vector3.Dot(totarget, totarget);
 
             float p = -b / (2 * a);
             float q = (float)Math.Sqrt((b * b) - 4 * a * c) / (2 * a);
 
             float t1 = p - q;
             float t2 = p + q;
             float t;
 
             if (t1 > t2 && t2 > 0)
             {
                 t = t2;
             }
             else
             {
                 t = t1;
             }
 
             Vector3 aimSpot = targetPos + targetVelocity * t;
             Vector3 bulletPath = aimSpot - Ship.Position;
             float timeToImpact = bulletPath.Length() / bulletSpeed;//speed must be in units per second
 
             return aimSpot;
         }

EDIT Made a new easier formula, that should work mathematically with my limited knowledge. But seems to be malfunctioning as well. The new method was like this:

  private Vector3 GetDirectionToShoot(WorldObject target)
         {        
             Vector3 aimSpot = (target.Position - Ship.Position) * Ship.GunInfo.ProjectileSpeed + 
             target.LinearVelocity - Ship.LinearVelocity;
             return aimSpot;
         }
Comment
Add comment · Show 21
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 elenzil · Sep 20, 2019 at 06:12 PM 0
Share

i'm likely mis-understanding, but it sounds like you have some code which calculates the bullet trajectory for a moving target if the player is standing still, and need to modify it to account for the player moving ? if that's the case i think you would just subtract the player velocity from the target velocity before doing your calculation.

avatar image Captain_Pineapple elenzil · Sep 20, 2019 at 07:24 PM 0
Share

Na that wont work since the bullets velocity also get changed by the players velocity. it would work if the bullet velocity was always the same.

avatar image elenzil Captain_Pineapple · Sep 20, 2019 at 08:08 PM 0
Share

well right.

assu$$anonymous$$g you have something like

 vec3 calcBulletInitialVelocityForStationaryPlayer(vec3 playerPos, vec3 targetPos, vec3 targetVel) {...}

then you could write

 vec3 calcBulletInitialVelocityFor$$anonymous$$ovingPlayer(vec3 playerPos, vec3 playerVel, vec3 targetPos, vec3 targetVel) {
   return calcBulletInitialVelocityForStationaryPlayer(playerPos, targetPos, targetVel - playerVel) + playerVel;
 }
Show more comments
Show more comments

1 Reply

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

Answer by Simpowitch · Sep 20, 2019 at 11:54 PM

http://answers.unity.com/comments/1666745/view.html

However the code on the wiki is actually tested and works properly: http://wiki.unity3d.com/index.php/Calculating_Lead_For_Projectiles?_ga=2.83628886.131956639.1568980243-1605833034.1542013817#Calculating_the_intercept_point


From Bunny83.

Note. For me to make this work I had to subtract the currentPosition of the ship/player from the recived Vector3 from the method on this website. To further improve my targeting I searched for targets more towards the front of the ship/player, since shooting backwards is dependent on your bullet speed vs. player speed, which in my case often resulted in a miss since my ship speed was close to the same as the bullet speed.

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

207 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calculate force applied to rigidbody to make it land at specific location 2 Answers

realistic bullet trajectory 1 Answer

Please help, bullet drop is wrong at certain angles ? 0 Answers

Optimising projectile continuously calculated impact point 2 Answers

Drawing projectile trajectory 5 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