Reputation: 368
I've been working on a 2D game in Unity and I need to find a way to rotate a sprite around a certain point. I know that for 3D games, Unity has a built in transform.RotateAround()
function but I'm not sure how to achieve the 2D equivalent. If anybody can help, a response would be much appreciated.
Upvotes: 2
Views: 26164
Reputation: 207
actually you can use some GameObject as anchor:
public GameObject anchor;
public float velocidad;
void Start () {
velocidad = 50f;
}
// Update is called once per frame
void FixedUpdate () {
transform.RotateAround(anchor.transform.localPosition, Vector3.back, Time.deltaTime*velocidad);
}
Upvotes: 2
Reputation: 636
If you are struggling to get your head around these transformation functions, there is an alternative way to do it. Simple create a new gameobject at the point you wish to rotate around. Then make the sprite a child of that game object. When you rotate the game object the sprite should move about that point.
Upvotes: 5
Reputation: 457
You can use the same function. transform.RotateAround()
takes a Vector3 point
, a Vector3 axis
, and a float angle
in degrees.
the point and angle are pretty self explanitory, but the axis is a little less so. This is essentially the direction of rotation. In a default Unity2D game where z is your depth (into the screen), you'll want to rotate around the Z axis: new Vector3(0,0,1)
or Vector3.forward
.
try something like:
Vector3 point = new Vector3(5,0,0);
Vector3 axis = new Vector3(0,0,1);
transform.RotateAround(point, axis, Time.deltaTime * 10);
Upvotes: 4