Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by largow · Apr 14, 2014 at 05:46 PM · 2dmovementtransformcirclecircular

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!!

Comment
Add comment · Show 1
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 alikamii · Nov 24, 2016 at 05:19 PM 0
Share

and what should i do that my prefab use my spawner posiotion to create and move after that? @robertbu

1 Reply

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

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;
     }
 }
Comment
Add comment · Show 5 · 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 largow · Apr 14, 2014 at 06:31 PM 0
Share

Thanks!! It worked :)

avatar image largow · Apr 14, 2014 at 06:44 PM 0
Share

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!

avatar image robertbu · Apr 14, 2014 at 06:48 PM 0
Share

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;

avatar image $$anonymous$$ · Jun 26, 2015 at 02:48 PM 0
Share

Great answer. Finally THAN$$anonymous$$ YOU! I was watching over an hour to find this script. i just always found thinks for 3d object

avatar image FlightOfOne · Feb 12, 2016 at 04:25 PM 0
Share

thanks! helped me too :)

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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