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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Jan 09, 2016 at 02:46 AM · 2dmathangletrajectorylaunch

How can I launch a GameObject at a target if I am given everything except for its launch angle?

I have been working on this one for a few weeks, and I have yet to succeed. I have searched every resource out there, and tried them all, but still without success, so I do not believe existing links will be of much use to me at this point. I have purchased scripts from the Asset Store in hopes of finding some usable code there, but no such luck.

I am trying to launch an object at a target, given its position, its target position, the launch force, and the gravity. I am following this formula:

Formula

I have simplified the code to the best of my ability, but I still cannot consistently hit the target. (I am not actually launching the origin itself, but it seemed simpler than having code instantiating another GameObject.)

Does anyone know what I am doing wrong?

 using UnityEngine;
 
 public class Launcher : MonoBehaviour
 {
     public float speed = 10.0f;
 
     void Start()
     {
         Launch(GameObject.Find("Target").transform);
     }
 
     public void Launch(Transform target)
     {
         float angle = GetAngle(transform.position, target.position, speed, -Physics2D.gravity.y);
         var forceToAdd = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
         GetComponent<Rigidbody2D>().AddForce(forceToAdd, ForceMode2D.Impulse);
     }
 
     private float GetAngle(Vector2 origin, Vector2 destination, float force, float gravity)
     {
         float angle = 0.0f;
 
         //Labeling variables to match formula
         float x = Mathf.Abs(destination.x - origin.x);
         float y = Mathf.Abs(destination.y - origin.y);
         float v = force;
         float g = gravity;
 
         //Formula seen above
         float valueToBeSquareRooted = Mathf.Pow(v, 4) - g * (g * Mathf.Pow(x, 2) + 2 * y * Mathf.Pow(v, 2));
         if (valueToBeSquareRooted >= 0)
         {
             angle = Mathf.Atan(Mathf.Deg2Rad * (Mathf.Pow(v, 2) + Mathf.Sqrt(valueToBeSquareRooted)) / g * x);
         }
         else
         {
             //Destination is out of range
         }
 
         return angle;
     }
 }

Again, I have spent weeks on this problem. I am at the end of my mental rope. Please help!

formula.png (2.2 kB)
Comment
Add comment · Show 1
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 brunocoimbra · Jan 09, 2016 at 04:09 AM 0
Share

What did happen? The "Launcher"s object isn't moving? It is moving less than expected? Be more expecific about the problem, so we can help you.

Also, is there any other movement script attached to the "Launcher" object?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Jan 09, 2016 at 04:33 AM

I previously posted my general formulas for 3-dimensional trajectory, but it should translate quite easily to 2-dimensional, since it effectively just sets an axis difference to zero:

http://answers.unity3d.com/questions/1087568/3d-trajectory-prediction.html#answer-1087707

Edit: I specifically handle removing each of TimeToDestination, LaunchSpeed, and LauchAngle. As a result, you can utilize either of the first two to reach a destination accurately (or, perfectly with time or as close as possible for speed or angle).

Calculating based on any of those criteria cannot reasonably be combined with any other, as:

Time is dependent primarily on horizontal speed (which means a hard-set speed requirement modified by an angle to hit the destination point, adding vertical speed to the launch)

Speed can fall short of the destination when too little force is applied. Then, any sped beyond the minimum required (at a 45 degree angle) can be added either to reach the destination at a low angle or a high angle.

Angle can always reach the destination, provided the angle of the launch is not less than the angle to the destination.

Edit again: Typos corrected.

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

51 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

Related Questions

Angle between two vectors 4 Answers

Need help with calculating time required to pass a set distance. 0 Answers

How to get an object to turn to point to a direction using the shortest path. E.g. 15° to 345° in 30° instead of 330°. 0 Answers

Problem with Vector2/Vector3.Angle? 0 Answers

Math: How to rotate a wheel by drag&drop (Canvas, 2D)? 0 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