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
1
Question by ErrorX · Nov 27, 2014 at 03:25 PM · physicsmathangletrajectory

How to calculate the angle of a trajectory to hit the target

Hi everyone,

How do I calculate the angle of a trajectory to hit the target, without knowing the velocity. I only know the max height, offset height and the distance to the target.

alt text

This is what I got so far (I don't know how to calculate offset height ):

 float GetAngle(Vector3 startLocation, Vector3 endLocation, float maxHeight)
 {
         float distance = Mathf.Sqrt(Mathf.Pow(startLocation.x - endLocation.x,2) + Mathf.Pow(startLocation.z - endLocation.z,2));
  
         float offsetHeight = endLocation.y - startLocation.y;
         //how do I calculate offset height in this equation ?
         return -Mathf.Atan (4 * maxHeight/ distance ) + Mathf.PI;
 }

I use this to calculate the velocity (works fine I only need the correct angle):

alt text

 float LaunchVelocity (Vector3 startLocation, Vector3 endLocation, float angle)
 {
         float range = Mathf.Sqrt(Mathf.Pow(startLocation.x - endLocation.x,2) + Mathf.Pow(startLocation.z - endLocation.z,2));
         float offsetHeight = endLocation.y - startLocation.y;
         float gravity = Physics.gravity.y;
  
         float velocity = range * range * gravity;
         velocity /= range * Mathf.Sin(2 * angle) + 2 * offsetHeight * Mathf.Pow(Mathf.Cos(angle),2);
         return Mathf.Sqrt(velocity);
 }
math.jpg (46.9 kB)
Comment
Add comment · Show 5
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 Yalfbal · Nov 27, 2014 at 04:07 PM 0
Share

Is it mandatory that the projectile reaches the maximum height?

avatar image richyrich · Nov 27, 2014 at 04:13 PM 0
Share

To establish the Offset height, you could use Pythagoras... The hypotenuse can be deter$$anonymous$$ed by Vector3.Distance(start, target); which leaves one side remaining.

For theta, there's a similar post here, but its for 2D:

http://stackoverflow.com/questions/16701886/finding-initial-speed-and-angle-to-hit-a-known-position-parabolic-trajectory

avatar image ErrorX · Nov 27, 2014 at 04:51 PM 0
Share

Sorry Agorichyrich but I do not fully understand what you what you mean. Can you finish my GetAngle function?

avatar image swifteria · Nov 27, 2014 at 05:23 PM 0
Share

Use quanternions or something like that. I can't remember much about it, but it worked for me in many cases.

avatar image meat5000 ♦ swifteria · Nov 27, 2014 at 05:25 PM 0
Share

Quaternions just represent angles. They are not intuitive and I imagine that calculating trajectories would be easier in Euler.

1 Reply

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

Answer by koray1396 · Nov 27, 2014 at 05:09 PM

first, you should need to complete your code, otherwise you will always face similar problems.

I think there is no need to deal with complex formulas, I find simple steps helpful;

  • VerticalSpeed = Mathf.Sqrt(2*gravity*MaxHeight)

  • TravelTime = Sqrt(2*(MaxHeight - OffsetHeight) / g) + Sqrt(2*MaxHeight / g)

  • HorizontalSpeed = Distance / TravelTime

So your speed vector would be VerticalSpeed + HorizontalSpeed.

You can calculate the angle by Mathf.Atan2. Speed would be Magnitude of FinalVector.

Comment
Add comment · Show 6 · 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 Yalfbal · Nov 27, 2014 at 05:35 PM 0
Share

This is the correct answer. But, if the projectile doesn't have to reach the $$anonymous$$axHeight, there will be infinite angles to hit the target.

avatar image ErrorX · Nov 27, 2014 at 05:38 PM 0
Share

Thanks I understand it now

avatar image IntergalacticSloth · Apr 23, 2017 at 12:35 AM 0
Share
 void TrialShootBy$$anonymous$$axHeight(Rigidbody rigid)
     {
         Vector3 originOfLaunch = Vector3.zero;
         Vector3 targetPositionForLanding = Vector3.forward * 10;
         //$$anonymous$$ade these points up. Hope you change them!
         float maxPermittedHeight = 10f;
         //Not only does the code below find a trajectory...
         //The curve never goes higher than a provided height.
         //See the original question:
         //http://answers.unity3d.com/questions/841870/how-to-calculate-the-angle-of-a-trajectory-to-hit.html
 
 
         float heightDifference =
             originOfLaunch.y - targetPositionForLanding.y;
 
         float verticalSpeed =
             $$anonymous$$athf.Sqrt
             (2 * -Physics.gravity.y * maxPermittedHeight);
 
         float trialAndErrorFudgeICouldNotRemove = 0.06f;
         float expectedTravelTime = 0f;
         if ($$anonymous$$athf.Sign(heightDifference) == -1)
         {
             expectedTravelTime =
                 $$anonymous$$athf.Sqrt
                 (2 * (maxPermittedHeight - heightDifference) /
                 -Physics.gravity.y)
                 +
                 $$anonymous$$athf.Sqrt
                 (2 * maxPermittedHeight /
                 -Physics.gravity.y);
         }
         else if ($$anonymous$$athf.Sign(heightDifference) == 1)
         {
             trialAndErrorFudgeICouldNotRemove *= -1;
 
             expectedTravelTime =
                 $$anonymous$$athf.Sqrt
                 (2 * (maxPermittedHeight + heightDifference) /
                 -Physics.gravity.y)
                 +
                 $$anonymous$$athf.Sqrt
                 (2 * maxPermittedHeight /
                 -Physics.gravity.y);
         }
 
         float horizontalSpeed =
             Vector3.Distance
             (originOfLaunch, targetPositionForLanding)
             / expectedTravelTime;
 
         Vector3 facingTargetWithNoY =
             (targetPositionForLanding - originOfLaunch);
         facingTargetWithNoY.y = 0f;
 
         Vector3 normalizedForwardDir = facingTargetWithNoY.normalized;
         Vector3 launchVector = new Vector3
                 (normalizedForwardDir.x * horizontalSpeed,
                 verticalSpeed,
                 normalizedForwardDir.z * horizontalSpeed);
         //quote from the question page linked at the top.
         // If you want, "You can calculate the angle by $$anonymous$$athf.Atan2.Speed would be $$anonymous$$agnitude of FinalVector." ???
 
         //Now to add force. $$anonymous$$ake sure it has none already.
         rigid.AddForce(-rigid.velocity, Force$$anonymous$$ode.VelocityChange);
         //Launch
         rigid.AddForce
             (launchVector, Force$$anonymous$$ode.VelocityChange);
         //And add some of that fudge!
         rigid.AddForce
             (launchVector * trialAndErrorFudgeICouldNotRemove, Force$$anonymous$$ode.VelocityChange);
     }
avatar image IntergalacticSloth IntergalacticSloth · Apr 23, 2017 at 12:39 AM 0
Share

Got it working by adding a bit of fudge force, messing with the sign of gravity in the equation, but above all: I had to add an if statement that uses "heightDifference" (the offset) with a different sign if it is positive (meaning the origin is above the landing spot).

Hate to say, I actually have to go play with this link for a few hours now. http://answers.unity3d.com/questions/49195/trajectory-of-a-projectile-formula-does-anyone-kno.html Because I don't need to keep to a certain $$anonymous$$AX HEIGHT. I need to keep to a certain LAUNCH VELOCITY

But yeah, if you just need a trajectory, or need to keep below a certain height... my script interpretation of $$anonymous$$oray's theory works O$$anonymous$$

(You might also consider adding a "$$anonymous$$ range" for this whole function. If target is damn close - why lob when you can shoot right at it??)

avatar image Eno-Khaon IntergalacticSloth · Apr 23, 2017 at 07:50 AM 1
Share

With the appropriate algorithm, there's no need for an if statement checking whether there is a height difference.

You might want to check out this answer (http://answers.unity3d.com/questions/1087568/3d-trajectory-prediction.html#comment-1343095) I gave on the subject a while back. Specifically, there are examples included for multiple means of generating an optimal trajectory, including a fixed speed for the initial launch velocity.

Show more comments

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

8 People are following this question.

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

Related Questions

Drawing projectile trajectory 5 Answers

Bomb Trajectory for a "Water Canon" (Maths) 1 Answer

physics & rotation question 1 Answer

Finding the angle to bounce object to target 1 Answer

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


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