- Home /
Rotating Y towards X/Z
I would like to figure out the best way to rotate Y towards a target on the X/Z planes, such as clicking a tile and having a player rotate towards it - but only turning on Y.
Are there any recommended methods to work this out?
When a tile is clicked on, I'm not getting the mouse posistion but the tile posistion itself - which is in the world as opposed to screen space. Not quite the same problem :)
Answer by asafsitner · Feb 15, 2012 at 08:52 AM
Make a new GameObject as a child of the figure object (just for the sake of organized hierarchy, there's no special reason why it has to be a child of the figure object), and make the - let's call this helper game object 'LookHelper' - look at the tile (with LookAt()
method).
Now, in c# you'll have to grab the LookHelper.eulerAngles.y
value and assign it to a temporary Vector3 - let's call it tempRotation
- and just feed this vector into the figure's euler angles property i.e Figure.eulerAngles = tempRotation;
.
While in UnityScript you can just feed this value directly to the figure's euler angles property, i.e Figure.eulerAngles.y = LookHelper.eulerAngles.y;