- Home /
Multiple GameObjects moving in circular path
Hello guys, Iam making a 2d game in which i want to make some kind of "ring" composed of game objects that spin around the same centre and with the same radius..
(Watch this video I want to do the same that is done there with the apples).
The issue is that the script I ve done always set the same transform to all the gameObjects, so all of them are always 'aligned' and you will only see one of them . It doesn't matter if they were originally positioned apart from each other, once the script runs they start spinning but always in the same position...
Here's the code:
using UnityEngine;
using System.Collections;
public class CircularMovement : MonoBehaviour {
public Transform center;
public float radius = 5;
public float angle =0;
public float period = 6f;
// Update is called once per frame
void Update () {
angle += period*Time.deltaTime;
float x = Mathf.Cos(angle)*radius + center.transform.position.x; //x=cos(angle)*R+a;
float y = Mathf.Sin(angle)*radius + center.transform.position.y; //y=sin(angle)*R+b;
this.gameObject.transform.position = new Vector2 (x,y);
}
}
Thanks!!
and what should i do that my prefab use my spawner posiotion to create and move after that? @robertbu
Answer by robertbu · Apr 14, 2014 at 06:02 PM
Looking at the video, the apples keep their orientation as they rotate. That implies they are using position like you are doing in your code instead of the numerous other solutions for rotating around something. Here is a bit of code using a vector that implements what I see in the video. Note that I've removed 'radius', and simply use the starting position of the object with respect to the center to calculate the radius.
using UnityEngine;
using System.Collections;
public class CircularMovement : MonoBehaviour {
public Transform center;
public float degreesPerSecond = -65.0f;
private Vector3 v;
void Start() {
v = transform.position - center.position;
}
void Update () {
v = Quaternion.AngleAxis (degreesPerSecond * Time.deltaTime, Vector3.forward) * v;
transform.position = center.position + v;
}
}
Just to know how it works (cause i don't like copying and pasting without knowing what the script do), Quaternion.AngleAxis rotates a vector around a certain axis, but why do you multiply it * v?
Thanks!
Quaterion is a rotation. It does nothing by itself. For example, if you attempted to assign a rotation to a Vector3 like this:
v = Quaternion.AngleAxis (degreesPerSecond * Time.deltaTime, Vector3.forward);
...you'd get a compiler error since you cannot assign a Quaternion to a vector3. But if you multiply a Vector3 by a Quaternion, then you the result is a Vector3 rotated by the Queaternion. Order matters. You can do:
v = q * v;
...but this will generate and error:
v = v * q;
Great answer. Finally THAN$$anonymous$$ YOU! I was watching over an hour to find this script. i just always found thinks for 3d object
Your answer
Follow this Question
Related Questions
Moving in circles 1 Answer
Stop Moving When Not Touching Screen? 1 Answer
Rotating an object towards target on a single axis 2 Answers
Problem when rotating sprite and moving it foward 1 Answer
Sprite moving too far/fast 1 Answer