Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
2
Question by chinvpl · Feb 17, 2017 at 10:24 PM · physicsaddforceprojectile

Getting launch angle for projectile given height, distance and speed in 3D

I am trying to calculate the launch velocity of a ball that needs to land at a certain position in 3D space. I know the height the ball will be launched from, the position it needs to land and the initial magnitude of the velocity.

I want to produce a Vector3 that I can use with AddForce that propels the ball and lands on the target.

I have the following function that I use to work out the launch angle, based on equations in this video:

 private Vector3 GetLaunchAngle(float speed)
 {
     //Calculate angle to z-axis
     float height = transform.position.y; //Height from 0
     float distance = Vector3.Distance(new Vector3(transform.position.x, 0, transform.position.z), new Vector3(aim.transform.position.x, 0, aim.transform.position.z)); //Distance to target (aim)
     float angle;
     Vector3 launchAngle = Vector3.forward;

     float quadB = 0.5f * Physics.gravity.y * (distance / speed); //B in quadratic equation = 1/2g * (d/v)
     float quadC = quadB + height;  //C in quadratic equation

     float tanAngle1 = (-quadB + Mathf.Sqrt((quadB * quadB) - (4 * distance * quadC))) / (2 * distance); //Calculate + in quadratic equation
     float tanAngle2 = (-quadB - Mathf.Sqrt((quadB * quadB) - (4 * distance * quadC))) / (2 * distance); //Calculate - in quadratic equation

     //Calculate both possibilities of angle
     float angle1 = Mathf.Rad2Deg * Mathf.Atan(tanAngle1);
     float angle2 = Mathf.Rad2Deg * Mathf.Atan(tanAngle2);

     //If angle 1 is invalid
     if (angle1 > 90 || angle1 < -90 || float.IsNaN(angle1))
     {
         angle = angle2;
     }
     //If angle 2 is invalid
     else if (angle2 > 90 || angle2 < -90 || float.IsNaN(angle2))
     {
         angle = angle1;
     }
     //If both are valid get min angle
     else if (!float.IsNaN(angle1) && !float.IsNaN(angle2))
     {
         angle = Math.Min(angle1, angle2);
     }
     else
     {
         angle = float.NaN;
     }

     //Try to rotate correct angles
     if (!float.IsNaN(angle))
     {
         //Apply x-axis rotation
         launchAngle = Quaternion.AngleAxis(angle, Vector3.left) * launchAngle;
         //Calculate (x,y) angle to target
         float angleTarget = Vector2.Angle(new Vector2(transform.position.x, transform.position.z), new Vector2(aim.transform.position.x, aim.transform.position.z));
         //Set left/right rotation
         angleTarget = angleTarget > 90 ? angleTarget - 180 : angleTarget;
         //Apply y-axis rotation
         launchAngle = Quaternion.AngleAxis(angleTarget, Vector3.up) * launchAngle;
     }
     else
     {
         launchAngle = Vector3.forward;
     }

     return launchAngle;
 }

The main things that are wrong with this are, the (x,z) direction is off (see screenshot 1 below). I'm sure that the below is not the best way to do this:

 float angleTarget = Vector2.Angle(new Vector2(transform.position.x, transform.position.z), new Vector2(aim.transform.position.x, aim.transform.position.z));
 //Set left/right rotation
 angleTarget = angleTarget > 90 ? angleTarget - 180 : angleTarget;
 //Apply y-axis rotation
 launchAngle = Quaternion.AngleAxis(angleTarget, Vector3.up) * launchAngle;

Also, I think that the calculated angle of incline is correct but I am open to other ideas around that.

Final issue is with the rigidbody.AddForce(launchAngle * speed);. So the speed is used to calculate the launch angle, but when used as the impulse for the force it barely moves, it looks like screenshot 2. I am using the ratio of 1 unit to 1m in Unity and therefore used a value of 35 for the speed (assuming this to mean 35m/s or approximately 80mph). Upon keeping the speed for the calculation of the angle but changing the impulse of the force to 1000, the ball moves like screenshot 3. How do you get the correct impulse on the rigidbody?

What is the best method for this?

EDIT: I have changed the AddForce to use the different ForceMode values with speed (35). Impulse and VelocityChange look like screens 4 & 5, it has improved the look of the arc, but it is still out (Long and left). Force and Acceleration looks like screenshot 2.

Screenshot 1 Screenshot 1 Screenshot 2 Screenshot 2 Screenshot 3 Screenshot 3 Screenshot 4 Screenshot 5 Screenshot 5 Screenshot 5

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by chinvpl · Feb 20, 2017 at 09:39 AM

Using the answer found here I was able to get it working with the following code and called with Vector3 launchAngle = GetLaunchAngle(speed); rigidbody.AddForce(launchAngle, ForceMode.VelocityChange);

 private Vector3 GetLaunchAngle(float speed)
 {
     Vector3 toTarget = aim.transform.position - new Vector3(transform.position.x, transform.position.y - GetComponent<SphereCollider>().bounds.size.y, transform.position.z);
     Vector3 launchAngle = Vector3.up;

     float gSquared = Physics.gravity.sqrMagnitude;
     float b = speed * speed + Vector3.Dot(toTarget, Physics.gravity);
     float discriminant = b * b - gSquared * toTarget.sqrMagnitude;

     if(discriminant >= 0)
     {
         float discRoot = Mathf.Sqrt(discriminant);
         float tMax = Mathf.Sqrt((b + discRoot) * 2 / gSquared);
         float tMin = Mathf.Sqrt((b - discRoot) * 2 / gSquared);
         float tLowEnergy = Mathf.Sqrt(Mathf.Sqrt(toTarget.sqrMagnitude * 4 / gSquared));

         float time = tMin;

         launchAngle = toTarget / time - Physics.gravity * time / 2;
     }

     return launchAngle;
 }

Comparison shots are below: alt text alt text


cricket9.png (129.9 kB)
cricket10.png (302.6 kB)
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
avatar image
0

Answer by R33F · Feb 20 at 05:16 PM

Thanks man

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

6 People are following this question.

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

Related Questions

How to create parabola path for addforce() 2 Answers

AddForce has no effect on my cannonball. 3 Answers

Realistic player movement? 2 Answers

How to control easing in Mathf.smoothDamp? 1 Answer

Problem with when turning left with angling and the force of the projectile 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