Make RawImage on canvas look at gameObject
I have a RawImage that I'm treating as a sort of "compass" that will rotate to point at a gameObject. My problem is, the RawImage is part of my canvas and whenever I rotate it to look at the gameObject, it rotates on all three axises and when I lock it to just the Z axis, it still doesn't point to the object. I believe it's a problem with going from Vector3 (gameObject's position) to Vector2 (RawImage's position), but I still can't get it to point properly.
Thank you!
I think we need some more details. For example, how is the compass supposed to represent a 3d direction? A Real compass cannot: It can give you a direction, but that direction lies on the plane of the compass. So, if you held the compass flat, it would not be able to give you the elevation.
Along this line of though, I would have expected you to convert your Vector3 direction, into an ANGLE (rather than a Vector2).
Assu$$anonymous$$g the compass lies horizontally I would do this as follows:
Simply ignore you elevation-axis, and convert the object direction into a Vector2 that represents a purely-horizontal direction. (only copy the non, elevation coordinates)
Then perform the angle computation on that Vector2 (e.g. compasAngle=Vector2.Angle(noElevationVector2,Vector2.up) )
The angle that results would be how much to rotate your image: e.g. image.transform.localRotataion = Quaternion.Euler(0,0,compasAngle);
note: If you want a '$$anonymous$$i-map' you could also scale the resultant noElevationVector2, to the $$anonymous$$i-map view size, if you wanted to draw a dot for the object on the image.
Edit: note- the coordinates I used in the examples, might not be appropriate for your setup.
Thank you. Your assumption would be correct. I'm making a top down game and I'm using the image to point in the direction that the objective is in. Let's say the objective is to go get an item, I'm trying to get the "compass" to point in the direction of the item so that the player knows which way to go.
EDIT: I tried the code that you provided, and it works to an extent. It is only accurate when the object is on the left side of the image. If the object is to the right of the image, however, the arrow mirrors what it should be, pointing North-West ins$$anonymous$$d of North-East. It also only updates when the objective is moved, not when the camera moves (the player moving around the map).
" only updates when the objective is moved, not when the camera moves (the player moving around the map)."
when I mentioned the "object direction" above, this can be interpreted to mean a few different things, it could be the object's absolute position, or it could be the objects position RELATIBE to the player. To get the direction (and distance) from the player to the object (relative), simply subtract position vectors e.g. playerToObjectVector3 = object.transform.position - player.transfrom.position
regarding it flipping around the wrong way, I'm not quite sure.. but perhaps I use the wrong relative direction when I suggested Vector2.up? It's also possible the Vector2.Angle function is only returning positive values. $$anonymous$$aybe try using Atan2, ins$$anonymous$$d, to get the angle:
compasAngle=$$anonymous$$athf.Atan2(noElevationVector2.x,noElevationVector2.y);
Your answer
Follow this Question
Related Questions
Rotat a GameObject and than shoot it in the New (x) direction 1 Answer
How to properly set rotation constraints in the positive and negative direction? 0 Answers
How to get a vector 3 to vector 2 - c# 1 Answer
Operator '==' is ambiguous on operands of type 'Vector2' and 'Vector3' 1 Answer
New Vector2 won't take effect when called from another script in the same GameObject. 0 Answers