Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Seranach · Jan 07 at 09:49 PM · velocityprojectiletrajectory

implementing projectiles - angle of impact

hey guys,

Im implementing a long range artillery mechanic in my game. It is to be relatively realistic and for this, Ive been basing most of what Ive been doing on these links:

[https://www.forrestthewoods.com/blog/solving_ballistic_trajectories/] [https://vilbeyli.github.io/Projectile-Motion-Tutorial-for-Arrows-and-Missiles-in-Unity3D/] [https://en.wikipedia.org/wiki/Projectile_motion]

Ive implemented the "Angle θ required to hit coordinate (x, y)" part of these calcs.

Ive gotten it working well enough and it works quite well - except the projectiles are not hitting the mark - they are barely missing consistently and exactly.

Heres the code for the angle calculation:

     public float CalcAngle(Transform mark, Transform shellSpawn)
     {
         Vector3 diff = mark.transform.position - shellSpawn.transform.position;
         float distH = diff.y;
         //float distH = 0;
         Debug.Log(distH);
         float distL = Vector3.Distance(shellSpawn.transform.position, mark.transform.position);
 
         float v = _ShellVelocityMS;
         float g = Physics.gravity.y;
         float x = distL;
         float y = distH;
 
         float v2 = v * v;
         float v4 = v * v * v * v;
 
         float gx2 = g * x * x;
         float yv2 = 2 * y * v * v;
         float gx = g * x;
 
         float res = Mathf.Sqrt(v4 - g * (gx2 + yv2));
         float resFlat = (v2 - res) / gx;
         float resHigh = (v2 + res) / gx;
 
         resFlat = Mathf.Atan(resFlat) * Mathf.Rad2Deg;
         resHigh = Mathf.Atan(resHigh) * Mathf.Rad2Deg;
 
         float angle = resFlat;
 
         if (float.IsNaN(angle))
             angle = _IdleAngle * -1;
 
         diff = diff.normalized;
         diff = (diff * Mathf.Cos(angle) * v) + (Vector3.up * Mathf.Sin(angle) * v);
         //Debug.Log(diff);
 
         return angle;
     }

I have two cannons - both shooting at different elevations: cannon1 launches at 2 meters height cannon2 launches at 1 meter height

at flat trajectories, cannon 1 overshoots massively, cannon 2 half as much. At a high trajectory, they still overshoot - not at the same level, but they stay in relative relation to each other.

Setting distH from a positive to a negative value pulls the landing sites of the shots closer to the cannons, as expected - but the same discrepancy is still to be had. Something is not implemented correctly in my calculations and Im stumped .... would be great if yall could throw a look over it.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Seranach · Jan 07 at 09:59 PM

Also, ignore the [diff] variable at the end, was the rest of something I was working on.

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 Seranach · Feb 06 at 11:00 AM 0
Share

update:

instead of using my code, I straight-up copied the code from [https://github.com/forrestthewoods/lib_fts/blob/master/code/fts_ballistic_trajectory.cs] namely the function at 285 (which is the code for this article [https://www.forrestthewoods.com/blog/solving_ballistic_trajectories/]) which shows the code in-action doing precisely what I need.

aaaand im getting exactly the same result as with my code .... which is absolutely insane. Im literally using a function that is proven to be correct - and im getting an incorrect outcome.

If i ever find out what is buggering here, ill give an update, of course

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

175 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

Related Questions

Projectile trajectory based on angle 0 Answers

Instantiating prefabs, and making them move. (Projectiles) 0 Answers

How to make Cool Goal physics 0 Answers

Calculating projectile lead - improving accuracy 0 Answers

My projectile isn't getting fired with any velocity 2 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