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

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

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.

using UnityEngine;
using System.Collections;

public class RotateScript : MonoBehaviour {

    public Transform ring1, ring2;

    void Update () {
        float rot1 = 0, rot2 = 0;
        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;
        ring1.Rotate(rot1 * Time.deltaTime, 0, 0);
        ring2.Rotate(0, 0, rot2 * Time.deltaTime);
    }
}

thanks Zaflis I will give it a go

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 ?


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

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

Did you make sure they are parented to each other? As thats whats happening in the picture.

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

yes each is a child of the ring larger that itself

They will rotate around their center, just change that in the scene view.

I don’t know how to charge it

Use the arrows. Or change the x, y, z values in Position.

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

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. :sweat_smile:

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.

1 Like

That is closer but very hard to line them up

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

Not many changes done, Clamp() does the speed limit for min and max rotation.

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

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.

1 Like

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

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.