So I'm trying to make a simple controller where a circular sprite rotates (or orbits) around another circular sprite. The orbital direction is controlled by left or right on the keyboard. I've been looking into Transform.RotateAround, but I've only gotten the orbiting sprite to rotate itself and not actually orbit around the target sprite.
Here is what I did:
(The gameobjects have been properly assigned in the editor)
public GameObject target;
public GameObject orbit;
public float angle;
void Update() {
orbit.transform.RotateAround(target.transform.position, Vector2.up, angle);
}
As far as my understandment of RotateAround is concerned, the orbit object should orbit around the target gameobject by providing its transform position, but the orbit object is stationary and rotates itself instead of orbiting. I must be doing something wrong here.
RotateAround won't produce an orbit because an orbit consists of an axis of rotation, plus an offset. RotateAround gives you the axis, but not the offset.
What your line of code there says is, "make Vector2.up pass through the origin of target, and rotate orbit around this axis by angle". There's no place here to specify an offset, or more accurately, a radius.
I did a quick search and found this example on Unity answers,
Orbit with Unity3D
If you're determined to use RotateAround, I'm sure there's a way. Maybe you could make another gameobject and make target a child, then offset target from the center of this new gameobject, then use RotateAround but apply it to the gameobject. Maybe I'll give it a go when I get home from work.
I've been looking into the Quaternion method outlined in the video and I pretty much copy/pasted the code, but Visual Studio returns errors such as:
"'Quaternion' does not contain a definition for 'LookRotation'"
"Cannot implicitly convert type 'UnityEngine.Quaternion' to 'Quaternion'"
"'Quaternion' does not contain a definition for 'Slerp'"
I have upgraded to Unity 5, but I'm not sure why it doesn't recognize that I'm using the quaternion classes.
Can you post a snippet of your code?
Here it is:
using UnityEngine;
using System.Collections;
public class Gravity : MonoBehaviour {
public Transform target;
void Update () {
Vector3 relativePos = (target.position + new Vector3(0, 1.5f, 0)) - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
Quaternion current = transform.localRotation;
transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime);
transform.Translate(0, 0, 3 * Time.deltaTime);
}
}
Edit: Now it somehow works and I didn't do anything.
Edit2: The class must've tried to refer to itself because I did originally call it Quaternion (whoops)
I'll see if I can get it working
So, did you get it working?