- Home /
How to find the direction an object is flying in?
I have a golf ball that is spinning on all the different axis and I need to find out which angle it is heading in on the Y axis. Transform.forward doesn't work because it is spinning.
What do you mean by "heading"? It implies it has some velocity, right? In that case, isn't the velocity vector of the object your direction of movement?
Answer by Pangamini · May 22, 2019 at 09:28 AM
Ah now I understand the problem By 'angle on the Y axis', you mean, like, the azimuth. First, the rotation of the ball has nothing to do with its movement. The ball has a velocity (either from rigidbody, or from your own script), which I will assume is a world-oriented Vector3. If that is the case, and what you want to calculate is the azimuth, then you need to: 1. Take the X and Z elements of the velocity (the Y element doesn't affect the azimuth, right?) 2. Feed it to the Mathf.Atan2. It will calculate an angle of a vector from the X+ axis in CCW direction. See the docs for that function. The result is in radians, make sure you convert to the units you need.
Answer by tormentoarmagedoom · May 22, 2019 at 07:48 AM
Hello.
[Edited] : Pangamini comment is correct, is a better (less cpu cost) way.
Maybe there is some command or fucntion to know it.
But if you dont find any, you can always make a "trick":
In a script, during the same frame...:
Create a emptyobject, child of the ball with localposition and localrotation of 0.
Move this emptyobjc in Y axis of its local coords. Then, unparent it from the ball.
Then calculate the vector (emptyobj position - ball position).
This vector is the Y direction of the ball.
Bye!
how would I create the empty gameobject as a child in script?
Come on max... make little research... its simple. (you can always Instantiate a prefab to do it)
sorry i'm pretty new to the scene and only an 11 year old kid
Creating a gameObject and destroying it immediately for a simple calculation doesn't sound like a good idea You can calculate the exact same thing by taking the ball's transform
Vector3 worldDirection = ball.transform.TransformDirection(localDirection)
that's what I thought, destroying and recreating it would possibly be demanding on the performance of the device
$$anonymous$$ostly I try to avoid ANY kind of memory allocation in the updates ( or anywhere possible) because unity's garbage collector sucks, and it causes the fps hiccups once a while. The more memory garbage, the more often it happens. That's why Unity keeps adding methods that, ins$$anonymous$$d of returning an array of something (Eg. raycast hits) they let you provide the buffer to be filled with data ins$$anonymous$$d. Ideally, you'd check the profiler and see no allocations over time, except for when you are really creating new objects in the scene. And even there, It's often good to pool and reuse the objects that are spawned frequently (projectiles, hit particles, etc), not because of the object creation cost, but the memory garbage collection cost.
Your answer
Follow this Question
Related Questions
transform.forward not working correctly? 1 Answer
Vector3.SignedAngle wrong direction when crossing the 0 point 1 Answer
Objects Rotation to modify Objects transform velocity/Direction of travel 1 Answer
Rotate player in movement direction 1 Answer
How to make a player rotate in a direction with a slope of the ground. 0 Answers