- Home /
How rotate only z axis of Gameobject (2D)
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!
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. $$anonymous$$y guess is that you have more than one script (or more than one rotation call) impacting the rotation.
now, i achieved rotate the gameobject,with Transform.lookAt, to follow the player, but now, the renderer its buggy and dont show the image this its how have to look
and this its buggy sprite
and this its the code for rotate:
public class Enemy : $$anonymous$$onoBehaviour {
public float angle;
public Transform iss;
void Update ()
{
//angle = $$anonymous$$athf.Atan2 (player.transform.position.x, player.transform.position.y)*$$anonymous$$athf.Rad2Deg;
iss.transform.LookAt (player.transform,Vector3.forward);
}
Answer by robertbu · Feb 20, 2014 at 05:50 AM
LookAt() 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 co$$anonymous$$g 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!
Thanku 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
hi my 2d sprite character rotate in hit point but character rotate in turn + move and hit direction to move now my character directly hit point to rotate.
code hare Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit = new RaycastHit(); if (Physics.Raycast (ray, out hit, 100)) { Vector3 hitPoint = hit.point;
float dist = Vector3.Distance(hitPoint, transform.position);
Debug.Log(dist);
targetDir = hitPoint - transform.position;
float angle = $$anonymous$$athf.Atan2(targetDir.y, targetDir.x) * $$anonymous$$athf.Rad2Deg;
//transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, angle), 20 * Time.deltaTime);
//Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
//transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 1);
}
plz help me..
Your answer
Follow this Question
Related Questions
Write a C# script to record GameObject rotation? 4 Answers
How get the rotation of an GameObject? 3 Answers
Instantiate GameObject towards player 0 Answers
Adding Rotation to a Gameobject 1 Answer
Rotation Jumping values (0 to 180) 1 Answer