- Home /
2D rotation around a object
What I'm wanting is to have a object rotate around another object in a circle in 2D basically having the object rotate at a certain speed though I'm not sure on how to do something like this.
Most of the stuff I found was how to rotate a object around itself when what i want is a object to rotate around another object/
Any help?
Answer by milleniu · Jan 26, 2016 at 12:24 PM
Hi, perhaps there's another way I don't know, but I'm curently using this in my project :
using UnityEngine;
using System.Collections;
public class DoRotateAround : MonoBehaviour {
public float speed;
public Transform target;
private Vector3 zAxis = new Vector3(0, 0, 1);
void FixedUpdate () {
transform.RotateAround(target.position, zAxis, speed);
}
}
It's using the rotateAround function, and since you're in 2D, you only need to use zAxis for the rotation.
You can choose the center of the rotation with target and speed with. Passing from clockwise to counter clockwise rotation just require a negative speed.
I tried using that function but ins$$anonymous$$d of rotating around the object, they rotate around themselves.
How did you use the script ?
For example if you want _objectA to rotate around _objectB, you need to put the above script on _objectA, and set target to _objectB in the inspector.
@milleniu hey! I know this is old, But I was wondering if you know a way to do this, but instead of the object rotating around with a fixed speed, is there a way to make it rotate towards the mouse?
Answer by SterlingSoftworks · Jan 26, 2016 at 06:27 AM
Is this what you're trying to go for? (sorry that it's a .zip.. Wouldn't accept my .gif of this for some reason)
If so, add a rigid body to both objects you're wanting to have this happen with. On the stationary ( or the one you want the other object rotating around) add a "Hinge Joint 2D" and drag the object that will be rotating into the "Connected Rigid Body" slot. If you want it to continuously rotate, enable the "Motor" and set it to the speed you want. Otherwise when it's hit (when it has a collider) it will rotate freely based on it's mass.
Haveeeeee fun :D (if this is what you were wanting)
Answer by sIothman · May 28, 2016 at 03:51 PM
We'll try to use what milleniu suggested. After all, knowledge is never enough. But, I achieved to build a trap rotating with a Hinge Joint 2D component and changing the properties.
Answer by triangle4studios · Jan 20, 2021 at 10:30 PM
Rotation in 2D is almost the same as Rotation in 3D.
I have quickly thrown together a script and this 2 minute video to help you get started.
You can also refer to the Unity RotateAround API which is basically what this script below is doing, but the video explains how to do it in 2D.
using UnityEngine;
public class Orbit2D : MonoBehaviour
{
public GameObject target;
public float speed = 10;
public Vector3 direction = Vector3.up;
void Update()
{
transform.RotateAround(target.transform.position, direction, speed * Time.deltaTime);
}
}
oh, uh, this page is really old lol. I just needed help with something similar to this, so I put a comment in the top question (you can read it if you want)
Are you meaning, if you move the mouse, the object then rotates around the target object?
yes, but it rotates around the other object at a set distance from it, and points towards the area closest to the mouse. (I'm using the object to indicate the direction the player will dash, which is towards the mouse, so the object i'm talking about rotating will be rotating around the player, and needs to point to the mouse)
Your answer
Follow this Question
Related Questions
2d Movement Messes up when cube is rotatated 0 Answers
Basic 2D movement C# - Key presses cancel eachother out 4 Answers
Rotating an object towards target on a single axis 2 Answers
How should I move a sprite in a direction based on the rotation of a child? 1 Answer
making an object move to a certain point 3 Answers