- Home /
How can I create objects that are placed along the circumference of a circle,and then expand and contract the circle
I'm just starting to learn unity2d and I'm trying to create rotating obstacles that expand and contract.The obstacles are supposed to be in the pattern of a circle,which needs to keep rotating. I first created multiple objects and then added the following code to each,with each object having a different angle. Here is the code I've got for rotation and the expansion.But there is something wrong with it that makes the looping go wrong.
void Update()
{
if (radius < 1) {
angle += speed * Time.deltaTime;
radius += speed * Time.deltaTime;
}
if (radius > 5) {
angle -= speed * Time.deltaTime;
radius -= speed * Time.deltaTime;
}
x = Mathf.Cos (angle) * radius;
y = Mathf.Sin (angle) * radius;
transform.position = new Vector3 (x,y, -5);
}
You probably want to use different speed values when computing your angle, and radius changes.
I'm not sure what the if(radius<0) is supposed to detect- don't you always want to have a (radius >0)? If this is to check the $$anonymous$$imal size of the circle, I don't see where you check for the maximum size of the circle radius?
$$anonymous$$ay I suggest, that ins$$anonymous$$d of if-statements, you use another SIN function to add/subtract a cycling amount from your radius.
e.g:
angle += speed * Time.deltaTime;
float sin_value = $$anonymous$$antf.Sin(angle);
float current_radius = base_radius + (radius_flux_size * sin_value);
float x = $$anonymous$$athf.Cos (angle) * current_radius ;
float y = sin_value * current_radius ;
transform.position = new Vector3 (x,y, -5);
EDIT: hmm, if each object has a different angle value, might need to use a common variable to for the angle used to compute the radius change, I'll use Time.time in the example below.
angle += speed * Time.deltaTime;
float sin_value = $$anonymous$$antf.Sin(radius_speed * Time.time);
float current_radius = base_radius + (radius_flux_size * sin_value);
float x = $$anonymous$$athf.Cos (angle) * current_radius ;
float y = $$anonymous$$antf.Sin(angle); * current_radius ;
transform.position = new Vector3 (x,y, -5);
Yup.Thanks a lot @Glurth It works perfectly now.
Answer by Glurth · Oct 02, 2015 at 07:00 PM
You probably want to use different speed values when computing your angle, and radius changes.
I'm not sure what the if(radius<0) is supposed to detect- don't you always want to have a (radius >0)? If this is to check the minimal size of the circle, I don't see where you check for the maximum size of the circle radius?
May I suggest, that instead of if-statements, you use another SIN function to add/subtract a cycling amount from your radius.
e.g.
angle += speed * Time.deltaTime;
float sin_value = Mantf.Sin(radius_speed * Time.time);
float current_radius = base_radius + (radius_flux_size * sin_value);
float x = Mathf.Cos (angle) * current_radius ;
float y = Mantf.Sin(angle); * current_radius ;
transform.position = new Vector3 (x,y, -5);
Answer by green-giant · Oct 02, 2015 at 07:24 PM
public float angleOffset = 0;
float rotateSpeed = 5f;
float maxRadius = 0.5f;
float minRadius = 0.25f;
float oscillateSpeed = 0.1f;
float radius = 0.5f;
float angle;
float t = 0;
void FixedUpdate ()
{
t += oscillateSpeed;
var v = Mathf.Sin(t); //gives you a value between -1 and 1
v = (v + 1) / 2; //v is now between 0 and 1
v = minRadius + v * (maxRadius - minRadius); //
radius = v;
print(v);
angle += rotateSpeed;
var rotation = Quaternion.AngleAxis(angle + angleOffset, Vector3.forward);
transform.position = rotation * (Vector3.right * radius);
}
Your answer
Follow this Question
Related Questions
Objects rotator (rotate objects around a circle) 2 Answers
Determine Rotation Between Two Points 2 Answers
Smooth transition from current rotation to one in animation keyframe. Is it possible? 1 Answer
Animator recording and playing 2D sprite bone rotation in the wrong direction 0 Answers
Rotate around a circle 0 Answers