#Direction indicator
1 messages · Page 1 of 1 (latest)
You can get the movement direction by either rotating the direction from the player to the center by 90 degrees, or by taking the cross product of said direction and the outward direction (meaning the direction toward the user)
rotate the vector from center to the player by 90 degrees and set the arrow's up or right transform direction to that vector, depending in which local axis the arrow points
var centerToPlayer = player.position - center.position;
https://forum.unity.com/threads/whats-the-best-way-to-rotate-a-vector2-in-unity.729605/
you could also deal with an angle instead, and convert that to a direction
https://stackoverflow.com/questions/18851761/convert-an-angle-in-degrees-to-a-vector
Regarding that link, if you want to rotate a 2D vector by 90°, the easiest solution is probably to swap its x and y values.
oh right, I forgot about that ^^
just grabbed the first link I could find
The problem isn't rotating, Sorry about the confusing question. It was rather calculating that direction. 😶
For example if my player is at point A and the next point will be B, How can I calculate the point B? And then from these two I can find the proper direction of the arrow, Right?
Fair enough.
My first thought was to multiply with Quaternion.Euler(0,0,90) 
yep that would have worked as well ^^
Quaternion rotationStep = Quaternion.Euler(0f, 0f, rotationAngleSpeed * Time.deltaTime);
var newPosition = rotationStep * centerToPlayer + center.position;
basically:
first we consider the pivot around where we rotate: center.position
get the relative vector from that pivot to the player (point a) current position
then you can rotate that vector by the rotation you expect to perform.
We assume here that your game is a 2d game, playing on the x and y axis, meaning the relevant rotation axis is z
so you can calculate the delta rotation for the next frame (in this case, otherwise remove the deltaTime multiplication)
then you can rotate a vector in unity by doing quaternion * vector
so we rotate the vector from center to the player
since the vector is still a relative vector from the center, for the absolute position, we need to add the pivot again: center.position
you could do that with simpler trigonometry too
calculating the angle the player has to the center
adding or subtracting from that angle
turning that angle back into a vector direction again, and multiplying it with the magnitude of the vector from center to player to get a result vector with the same length
and the last step is the same as before, adding the center.position back
This makes more sense (To me maybe 😁 ), I googled how to find the points on circumference of circle and got this:
x = cx + r * cos(a)
y = cy + r * sin(a)
Where r is the radius, cx,cy the origin, and a the angle.
But couldn't find any info on how to find the current angle, If angle can be found then I think next position can be found using
Vector3 nextPosition = new Vector3(Mathf.Cos(angle) * radius, 0, Mathf.Sin(angle) * radius) + pivot.position
yeah that works too
current angle is either Mathf.Atan2, or you use Vector2.Angle
if you want a different reference direction for the position
(also note when doing angle calculations like that, you deal in radians, not eulerAngles
Like this?
float currentAngle = Mathf.Atan2(objectToRotate.transform.position.z - target.position.z, objectToRotate.transform.position.x - target.position.x) * Mathf.Rad2Deg;
You would need to be using .y instead of .z because you are working in 2D, but yes.
ah right
I'm working in 3D
so your axis you deal with are x and z instead of x and y?
and the rotations you need are along the y axis?
well that changes a little bit of what I've written above, so the quaternion would take a y rotation
otherwise,
if you add to currentAngle to get your desired angle, don't forget to do * Mathf.Deg2Rad again before you use the angle for Cos/Sin
It's kind of working now, But there's too much offset(I don't know what to call it). 😬
Can you come to stream and see what's happening?
no, I won't
🥺
``x = cx + r * cos(a)
y = cy + r * sin(a)
the r here,
so cx is center.x
r should be Vector3.Distance(objectToRotate.transform.position, target.position);
assuming both points have the same y position
Let me try this, I think the offset I was talking about was due to the fact that both points didn't have the same y.
if there is a different y position between objects, you can project the vector so as if the y positions were the same on both objects
var deltaVector = objectToRotate.transform.position - target.position;
var yProjVector = Vector3.ProjectOnPlane(deltaVector, Vector3.up);
float radius = yProjVector.magnitude;
or simply
float radius = new Vector2(objectToRotate.transform.position.x - target.position.x, objectToRotate.transform.position.z - target.position.z).magnitude;