#Direction indicator

1 messages · Page 1 of 1 (latest)

uncut valve
#

Hi, I want the player to move anticlockwise from a point at certain radius, for which I want to show him a direction arrow. How can I rotate the arrow in that specific direction the player has to move?

mental kayak
#

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)

light maple
mental kayak
light maple
#

oh right, I forgot about that ^^
just grabbed the first link I could find

uncut valve
#

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?

mental kayak
light maple
#

yep that would have worked as well ^^

light maple
light maple
# uncut valve The problem isn't rotating, Sorry about the confusing question. It was rather ca...

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

uncut valve
# light maple you could do that with simpler trigonometry too calculating the angle the player...

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
light maple
#

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

uncut valve
light maple
#

for a radian, a full rotation is 2*PI

#

I think something like this yeah

mental kayak
light maple
#

ah right

light maple
#

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

uncut valve
light maple
#

no, I won't

uncut valve
light maple
#

``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

uncut valve
light maple
#

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;