Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pulze_inc · Oct 02, 2015 at 05:14 PM · rotation2d animationcircleexpansion

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);
         
             
 
     }

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Oct 02, 2015 at 05:26 PM 0
Share

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.

avatar image Glurth Glurth · Oct 02, 2015 at 05:31 PM 0
Share

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);


avatar image pulze_inc Glurth · Oct 02, 2015 at 05:54 PM 0
Share

Yup.Thanks a lot @Glurth It works perfectly now.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges