Hi, i want to modify the value of the Z axis (rotation) of a Gameobject, without modify the other axis (x,y), i tried with: Transform.Rotate, Quaternion.Angleaxis, Quaternion.Euler, but with no results, i know this a noob question, but i cant figure out, how to do it. Thanks!
2 Answers
2LookAt() won’t work for sprites since LookAt() point the side facing positive ‘z’ towards the position you specify. This will turn the sprite sideways. The code you have comment out will not work for a couple of reasons. First, you have the parameters backwards. Atan2 should be called as Atan2(Y,X), but you are calling it Atan2(X,Y). Second, when you do the calculation, you need a direction. Unless the object you are rotating is at the origin, you are passing a position, not a direction to the Atan2(). So the angle returned is the angle from Vector2.zero, not from the object you are trying to rotate. I’m guessing some since I have incomplete information, but this is probably in the direction you need to go:
Vector3 dir = player.transform.position - iss.transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * mathf.Rad2Deg;
iss.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
that its what i was looking for, recently i figured out that its dint was a bug, it was the object rotating on (y,x), the parameters are inverted because i am coming from SDL, in sdl the order of axis its x,y, i only have a week on unity and c#, anyways, appreciate your answer, thanks!
– ReContraChanfleThanku roberty it helps for me also....i was searching for this from 2 days i tried quaternioin,lookat,euler angles but got fail everytime.Now i got it..Thanx a lot
– shwetakalraThanku roberty it helps for me also…i was searching for this from 2 days i tried quaternioin,lookat,euler angles but got fail everytime.Now i got it…Thanx a lot
All of the things you tried should have worked, so there is something else going on here. If you post your code, maybe we can figure out what is going on. My guess is that you have more than one script (or more than one rotation call) impacting the rotation.
– robertbu