Comments and answers for "Finding angle of angular motion"
http://answers.unity.com/questions/670962/finding-angle-of-angular-motion.html
The latest comments and answers for the question "Finding angle of angular motion"Comment by byerdelen on byerdelen's answer
http://answers.unity.com/comments/671834/view.html
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 : MonoBehaviour
{
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 instead 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 = Mathf.Sin(pGravity) * pVel;
//Distance
hDist = Vector3.Distance(transform.position, tPos);
//Vertical distance
yDist = tPos.y - transform.position.y;
//Horizontal distance
xDist = Mathf.Sqrt(Mathf.Pow(hDist, 2) - Mathf.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 = (Mathf.Atan((Mathf.Pow(pVel, 2.0f) - Mathf.Sqrt(Mathf.Abs(Mathf.Pow(pVel, 4.0f) - pGravity * (pGravity * Mathf.Pow(xDist, 2.0f) + 2.0f * yDist * Mathf.Pow(pVel, 2.0f)))) ) / (pGravity * xDist))) * Mathf.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.zipMon, 24 Mar 2014 12:36:59 GMTbyerdelenComment by Gruffy on Gruffy's answer
http://answers.unity.com/comments/671173/view.html
Additional:
Your "Fire1" is auto set to left mouse click btw.
:)
GruffySun, 23 Mar 2014 16:11:03 GMTGruffyComment by Gruffy
http://answers.unity.com/comments/671171/view.html
I love that link @Pako! nice one for that.
cheers bud. GruffySun, 23 Mar 2014 16:10:00 GMTGruffyAnswer by Gruffy
http://answers.unity.com/answers/671169/view.html
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);
}
}
}Sun, 23 Mar 2014 16:08:32 GMTGruffyComment by pako
http://answers.unity.com/comments/671144/view.html
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 Mathf.Asin((gravity x horizontal distance)/Mathf.Pow(startingSpeed,2))) * Mathf.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 (Mathf.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.Sun, 23 Mar 2014 15:39:55 GMTpakoComment by byerdelen
http://answers.unity.com/comments/671066/view.html
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?Sun, 23 Mar 2014 13:17:19 GMTbyerdelenComment by pako
http://answers.unity.com/comments/670997/view.html
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 mind, 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. Maybe 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.Sun, 23 Mar 2014 11:13:31 GMTpako