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
0
Question by byerdelen · Mar 23, 2014 at 09:30 AM · rotationphysicsprojectilemotionangular

Finding angle of angular motion

Hi,

I couldn't figure it out with my inadeuqute physics knowledge. I am firing an arrow with a certain speed to a specific target. I know my position, target position, speed of the projectile and the angular rotation of the projectile. With those, I am trying to find the starting angle of the projectile to reach the destination. It is a simple projectile motion problem but the projectile motion equation is not interested of the angular rotation, it is calculation according to the gravity so this equation is not worknig for me:

 Angle = (Mathf.Atan((Mathf.Pow(Vel, 2.0f) + Mathf.Sqrt(Mathf.Abs(Mathf.Pow(Vel, 4.0f) - Gravity * (Gravity * Mathf.Pow(xDist, 2.0f) + 2.0f * yDist * Mathf.Pow(Vel, 2.0f))))) / (Gravity * xDist))) * Mathf.Rad2Deg

Has anyone any idea to calculate the angle of a rotating arrow when the speed and the destination position is known? Thank you

Comment
Add comment · Show 4
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 pako · Mar 23, 2014 at 11:13 AM 0
Share

I can help you out with this, but first I need some clarifications. When in Physics we say that a body has an angle of rotation, we mean rotation about one of its axis. In this case, the angle of rotation of an arrow would be an angle about its long axis, resulting in spinning the arrow. With this in $$anonymous$$d, I don't understand what you mean by:

a. I know my ..., ... and the angular rotation of the angle b. Has anyone any idea to calculate the angle of a rotating arrow

I understand when you say "I am trying to find the starting angle of the projectile". So, "starting angle" is an obvious concept, but "angular rotation of the angle", and "angle of a rotating arrow", I don't understand. $$anonymous$$aybe you are using different different words to always refer to starting angle.

Having said that, while the projectile motion equations are not interested in the starting angle, you can calculate this angle if you apply the equations separately, to the vertical and horizontal components of the resulting velocity. For this you need some simple trigonometry, and although it may sound complicated, it's quite simple.

Here is the theory:

http://www.physicsclassroom.com/class/vectors/Lesson-2/Non-Horizontally-Launched-Projectiles-Problem-Solv

Have a look at the article, and let me know what you need and I'll walk you through it.

avatar image byerdelen · Mar 23, 2014 at 01:17 PM 0
Share

Hi Pako, I am sorry, the angular rotation of the angle means the angular rotation of the projectile. I fixed it above. I was not quite clear maybe. The calculation I am trying to achieve is to find the start angle of the projectile to reach a target with a known destination, speed and also a known decreasing of the angle of the object. I am using transform.forward in my calculations and it makes all the formulas not working because I am applying the vector to the arrow in its local direction. So for example my arrow goes 5 m/s and every second it is rotating lower for one degree. If it starts with 50 degree, it is being 49 degree after a second. So do you have any idea if this type of calculation is applicable to the projectile motion formulas with a change?

avatar image pako · Mar 23, 2014 at 03:39 PM 0
Share

Before getting into solving equations, I must clarify that "a known decreasing of the angle of the object" kind of complicates things (a lot). What you can do fairly easily, is find the starting angle for a known velocity of the projectile (arrow), in order to reach a known destination. During its flight, the projectile's velocity will have certain changing angles, which depend on the starting conditions (angle, velocity).

The starting angle in degrees can be found as follows:

 startingAngle = (0.5 x $$anonymous$$athf.Asin((gravity x horizontal distance)/$$anonymous$$athf.Pow(startingSpeed,2))) * $$anonymous$$athf.Rad2Deg;

In plain English the starting angle is equal to one-half the arc-sine of the product of (gravity times horizontal distance), divided by the square of the speed. Unity's arc-sine function returns radians, so you have to multiply the result by a Radians-to-degrees conversion constant ($$anonymous$$athf.Rad2Deg) to get the angle in degrees.

To find the angle at different points in the trajectory is quite complex. If you absolutely need it, I'll have some searching to do. However, the easiest way, I think, is to interpolate the angle of the arrow during its flight between known angles. e.g you now know the starting angle. You also know that at maximum height (when vertical velocity = 0) the arrow will be horizontal. Also if the target is at the same height as the shooter, the angle when the arrow hits the target will be the same as the starting angle.

avatar image Gruffy · Mar 23, 2014 at 04:10 PM 0
Share

I love that link @Pako! nice one for that. cheers bud. Gruffy

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Gruffy · Mar 23, 2014 at 04:08 PM

Hey bud. I think you could simplify your problem and use a rigidbody. If you take this script and add it to your camera or a parent gameobject of your camera. and add a prefab of your arrow to it in the inspector. MOST IMPORTANTLY, YOUR ARROW GAMEOBJECT SHOULD HAVE A RIGIDBODY COMPONENT ADDED TO IT. Sorry for shouting lol Gruffy Code below:

 using UnityEngine;
 using System.Collections;
 
 public class Shooter : MonoBehaviour 
 {
     GameObject currentCol;
     //declare and init shooter vars
     public Rigidbody bullet;  //add a prefab(arrow) MAKE SURE THE ARROW HAS RIGIDBODY COMPONENT ATTACHED TO IT 
     public float power = 1500f; //forward power
 
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetButtonUp("Fire1"))
         {
             //instantiate projectile - (overloads) what ?, where ?, a rotation ?
             Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody; 
             //vector to represent forward direction of the current transform
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             instance.AddForce(fwd * power);
             //Physics.IgnoreCollision (transform.root.collider, bullet.collider, true);
              //audio.PlayOneShot(bulletAudio);
         }
     }
 }
Comment
Add comment · Show 2 · 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 Gruffy · Mar 23, 2014 at 04:11 PM 0
Share

Additional: Your "Fire1" is auto set to left mouse click btw. :) Gruffy

avatar image byerdelen · Mar 24, 2014 at 12:36 PM 0
Share

Hi,

I do not want to use Unity's physics system to have complete control of the system so that is out of the option. Pako, thanks for your help. I have gone so far learning projectile motion and going through all the trigonometry equations but still things are not going well.

That is so much better for me to submit all the code for better understanding.

 using UnityEngine;
 using System.Collections;
 
 public class Projectile : $$anonymous$$onoBehaviour
 {
 
     public float speed = 1.0f;
     public float gravity = 1.0f;
     bool moving = true;
     float rAngle;
     Transform Target;
 
 
     public  IEnumerator shootit(Transform curtarget)
 
     {
 
         Vector3 randompos = curtarget.position;
 
         //Sending the values to the function for the angle calculation and rotating against the right angle
         CalculateShot(randompos, speed, gravity);
 
         while (moving)
         {
             //Applying velocity to the transform according to the local rotation
             transform.position += -transform.forward * speed;
             //Decreasing degree ins$$anonymous$$d of using a gravity so it will behave like a natural gravitational rotation
             transform.eulerAngles += new Vector3(gravity, 0, 0);
             yield return null;
         }
 
     }
 
 
     void OnTriggerEnter(Collider other)
     {
 
         moving = false;
 
     }
 
     void CalculateShot(Vector3 tPos , float pVel , float pGravity )
     {
 
         float xDist;
 
         float yDist;
 
         float hDist;
 
         //Changing the turning degree to the format of natural gravity such as 9.81 so the gravity equation will work correct
         pGravity = $$anonymous$$athf.Sin(pGravity) * pVel;
 
         //Distance
         hDist = Vector3.Distance(transform.position, tPos);
 
         //Vertical distance
         yDist = tPos.y - transform.position.y;
 
         //Horizontal distance
         xDist = $$anonymous$$athf.Sqrt($$anonymous$$athf.Pow(hDist, 2) - $$anonymous$$athf.Pow(yDist, 2));
 
         //Equation of projectile motion to find the angle
         //The source that the formula is taken as Angle required to hit coordinate (x,y) : http://en.wikipedia.org/wiki/Trajectory_of_a_projectile#Angle_required_to_hit_coordinate_.28x.2Cy.29
         //The code improved from here : http://forum.unity3d.com/threads/208397-Projectile-motion-strange-behavior
 
         rAngle = ($$anonymous$$athf.Atan(($$anonymous$$athf.Pow(pVel, 2.0f) - $$anonymous$$athf.Sqrt($$anonymous$$athf.Abs($$anonymous$$athf.Pow(pVel, 4.0f) - pGravity * (pGravity * $$anonymous$$athf.Pow(xDist, 2.0f) + 2.0f * yDist * $$anonymous$$athf.Pow(pVel, 2.0f)))) ) / (pGravity * xDist))) * $$anonymous$$athf.Rad2Deg;
 
         //Applying angle
         transform.localEulerAngles = new Vector3(rAngle, 180, 0);
 
     }
 
 }


The code based on the projectile motion code from here as the title Angle required to hit coordinate (x,y) : http://en.wikipedia.org/wiki/Trajectory_of_a_projectile#Angle_required_to_hit_coordinate_.28x.2Cy.29

Also I am attaching the 200 kbproject for better understanding that is simple and show out of the box. I would appreciate anyone who is pointing me the problem: https://dl.dropboxusercontent.com/u/35804228/Arrows%20example.zip

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

22 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

Related Questions

How to limit the motion direction of a GameObject? 1 Answer

Projectile motion with angle and force in one direction 1 Answer

Rotating projectile 2 Answers

Simple projectile motion with wind 0 Answers

A job task : Projectile Motion 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