Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How do I make a ring spin in 1 axis X or Y

Discussion in 'Scripting' started by Ruckrova, May 28, 2016.

  1. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    How do I make a ring spin in 1 axis X and a second ring inside the first in the Y axis ?

    I would like a C# script that can spin a ring in the x axis and spin a second ring in the y axis with the W S Keys for ring 1 and A D keys for Y .

    Sorry i can't write Scripts
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Rotate() takes 3 parameters, x, y, z rotation as angles. Swap them around as you prefer, or change + to - if it goes inverse and so on. Having keys set like this will make keys cancel eachother out, like pressing W and S at the same time causes ring to not rotate at all. Add this script to only 1 object, any object and set ring1 and ring2 in inspector.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RotateScript : MonoBehaviour {
    5.  
    6.     public Transform ring1, ring2;
    7.  
    8.     void Update () {
    9.         float rot1 = 0, rot2 = 0;
    10.         if (Input.GetKey(KeyCode.W)) rot1 += 10f;
    11.         if (Input.GetKey(KeyCode.S)) rot1 -= 10f;
    12.         if (Input.GetKey(KeyCode.A)) rot2 += 10f;
    13.         if (Input.GetKey(KeyCode.D)) rot2 -= 10f;
    14.         ring1.Rotate(rot1 * Time.deltaTime, 0, 0);
    15.         ring2.Rotate(0, 0, rot2 * Time.deltaTime);
    16.     }
    17. }
     
  3. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    thanks Zaflis I will give it a go
     
  4. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    It worked thanks but not as I hoped

    The rings do rotate but not around the centre of the rings , ring 1 is not far off but ring 2 is way off to the right side

    I need to rotate ring 2 in the middle of ring 1 , could it be the object location settings i have messed up ?
     
  5. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95

    Im trying to reproduce this in Unity so three rings it would seem
     
  6. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    I added a third ring



    using UnityEngine;
    using System.Collections;

    public class RotateScript : MonoBehaviour {

    public Transform ring1, ring2, ring3;

    void Update () {
    float rot1 = 0, rot2 = 0, rot3 = 0;
    if (Input.GetKey(KeyCode.W)) rot1 += 300f;
    if (Input.GetKey(KeyCode.S)) rot1 -= 300f;
    if (Input.GetKey(KeyCode.A)) rot2 += 300f;
    if (Input.GetKey(KeyCode.D)) rot2 -= 300f;
    if (Input.GetKey(KeyCode.Q)) rot3 += 300f;
    if (Input.GetKey(KeyCode.E)) rot3 -= 300f;
    ring1.Rotate(rot1 * Time.deltaTime, 0, 0);
    ring2.Rotate(0, rot2 * Time.deltaTime, 0);
    ring3.Rotate(0, 0, rot3 * Time.deltaTime);
    }
    }
     
  7. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Did you make sure they are parented to each other? As thats whats happening in the picture.
     
  8. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    the above script works with 3 rings as you would expect but i have the same issue with the ring 2 and 3 rotating around the wrong point
     
  9. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    yes each is a child of the ring larger that itself
     
  10. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    They will rotate around their center, just change that in the scene view.
     
  11. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    I don't know how to charge it
     
  12. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Use the arrows. Or change the x, y, z values in Position. ChangePos.png
     
  13. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    How do I move the pivot point and not the Object ? the pivot piont is not in the centre of ring 2 and 3 but is in 1
     
  14. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    You can't, what you can do though is create an empty GameObject, make the object (ring) you have a child of it. Set the GameObject to be the value of the ring in the script. Now the GameObject is the pivot.

    I hope thats clear enough, I can be quite vague at times. :oops:
     
  15. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    I was just about to say the same, but here's a picture. Make sure Pivot is selected in the top bar, not Center. Otherwise it depends on the 3D model on where the pivot is.

    unity_pivot.jpg
     
    RavenOfCode likes this.
  16. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    That is closer but very hard to line them up
     
  17. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    Ok thanks I can fiddle with it now to get them right

    Zaflis can you please add coding to make rings rotating faster if you out holding down the keys then stat at that speed until I hit the other key then slow down and rotate the opposite way ? or it that way more complex ? It would also need a reset / return to the right way up for all and stop
     
    Last edited: May 29, 2016
  18. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Not many changes done, Clamp() does the speed limit for min and max rotation.
    Code (CSharp):
    1.     public Transform ring1, ring2, ring3;
    2.  
    3.     float rot1 = 0, rot2 = 0, rot3 = 0;
    4.  
    5.     void Update() {
    6.         if (Input.GetKey(KeyCode.W)) rot1 += 10f;
    7.         if (Input.GetKey(KeyCode.S)) rot1 -= 10f;
    8.         if (Input.GetKey(KeyCode.A)) rot2 += 10f;
    9.         if (Input.GetKey(KeyCode.D)) rot2 -= 10f;
    10.         if (Input.GetKey(KeyCode.Q)) rot3 += 10f;
    11.         if (Input.GetKey(KeyCode.E)) rot3 -= 10f;
    12.         rot1 = Mathf.Clamp(rot1, -100, 100);
    13.         rot2 = Mathf.Clamp(rot2, -100, 100);
    14.         rot3 = Mathf.Clamp(rot3, -100, 100);
    15.         ring1.Rotate(rot1 * Time.deltaTime, 0, 0);
    16.         ring2.Rotate(0, rot2 * Time.deltaTime, 0);
    17.         ring3.Rotate(0, 0, rot3 * Time.deltaTime);
    18.     }
    19.  
    20.     void FixedUpdate() {
    21.         // Slow down
    22.         rot1 = rot1 * 0.96f;
    23.         rot2 = rot2 * 0.96f;
    24.         rot3 = rot3 * 0.96f;
    25.     }
    Demonstrating use of FixedUpdate too, as far as i know it's an event you don't need deltaTime or fixedDeltaTime necessarily. Simpler for multiplication, but you don't want to use Input's in FixedUpdate.

    Also afterthought, if you want to make the code a little shorter, you can get rid of all rot1, rot2 and rot3, and make Vector3 rot; instead.
     
    RavenOfCode likes this.
  19. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    using UnityEngine;
    using System.Collections;

    public class RotateScript : MonoBehaviour {

    public Transform ring1, ring2, ring3;

    float rot1 = 0, rot2 = 0, rot3 = 0;

    void Update() {
    if (Input.GetKey(KeyCode.W)) rot1 += 10f;
    if (Input.GetKey(KeyCode.S)) rot1 -= 10f;
    if (Input.GetKey(KeyCode.A)) rot2 += 10f;
    if (Input.GetKey(KeyCode.D)) rot2 -= 10f;
    if (Input.GetKey(KeyCode.Q)) rot3 += 10f;
    if (Input.GetKey(KeyCode.E)) rot3 -= 10f;
    rot1 = Mathf.Clamp(rot1, -100, 100);
    rot2 = Mathf.Clamp(rot2, -100, 100);
    rot3 = Mathf.Clamp(rot3, -100, 100);
    ring1.Rotate(rot1 * Time.deltaTime, 0, 0);
    ring2.Rotate(0, rot2 * Time.deltaTime, 0);
    ring3.Rotate(0, 0, rot3 * Time.deltaTime);
    }

    void FixedUpdate() {
    //Slow down
    rot1 = rot1 * 0.96f;
    rot2 = rot2 * 0.96f;
    rot3 = rot3 * 0.96f;
    }

    I got this to work thanks again . not quite what i was hoping for .

    The rings should keep turning until the [space] key is hit to stop them , then hit [r] key to reset the rings to their starting positions
     
    Last edited: May 29, 2016
  20. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    You should really look into the tutorials in the learn section. These are all pretty easy to do with just the knowledge from 1 or 2 of those tutorials.
     
  21. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    Thanks RavenOfCode I have done the roll-a ball one and learn alot for it , which one would you suggest that may help me with to create a Ring In blender and import it so it spins on the pivot point in unity ?
     
    RavenOfCode likes this.
  22. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    When using Unity alot of the things is just using your imagination. You are given a problem and you need to think of a way to fix it. There isn't a tutorial on everything so you need to know the basics and apply them to solve the problem. I would recommend at least doing the Roll a ball, Space Shooter, and the Survival Shooter. This is all I did and after that I just set to work creating games (granted they weren't the best, and the code was super messy, but they did work). The more you do and the more you learn the better and easier a creating something is.
     
  23. Ruckrova

    Ruckrova

    Joined:
    Nov 2, 2014
    Posts:
    95
    I will do the other two tutorials you mention thanks . BTW I got the gyroscope to work very well thanks for your and Zaflis help . There is so much to learn and I have so many ideas I would like to do , it's frustrating I can learn it faster.
    I had a look at the tank tutorial videos might get that a try too .
     
    RavenOfCode likes this.