Character Selection Rotation - How to remember previous rotation

Hi,

I have a basic character selection script, which also rotates the currently selected object, this is all working at it should, however, visually when you move left or right through the character selection, the transform rotation jumps ( or basically resets itself ) for each object, how might I go about applying the same rotation to all objects at the same time, so there is no jumping ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class characterSelection : MonoBehaviour {
    private GameObject[] characterList;
    private int index;

    [Header("Turn Table Properties")]
    public bool turnTable = true;

    public bool rotateX = false;
    public bool rotateY = false;
    public bool rotateZ = false;

    public float rotationSpeed = 25.0f;

    private void Start() {
        index = PlayerPrefs.GetInt("characterSelected");

        characterList = new GameObject[transform.childCount];

        // Fill the array with our models ( all children of parent )
        for (int i = 0; i < transform.childCount; i++)
            characterList[i] = transform.GetChild(i).gameObject;

        // Toggle off their renderer
        foreach (GameObject go in characterList)
            go.SetActive(false);

        // Toggle on the selected character
        if (characterList[index])
            characterList[index].SetActive(true);
    }

    private void Update () {
        if (turnTable == true) {
            if(rotateX) {
                characterList[index].transform.Rotate (rotationSpeed * Time.deltaTime,0,0);
            }
            if(rotateY) {
                characterList[index].transform.Rotate (0,rotationSpeed * Time.deltaTime,0);
            }
            if(rotateZ) {
                characterList[index].transform.Rotate (0,0, rotationSpeed * Time.deltaTime);
            }
        }
    }

    public void toggleLeft() {
        // Toggle off the current model
        characterList[index].SetActive(false);

        index--; // Alternatives : index -=1; index = index - 1;
        if (index < 0)
            // Make sure we are not out of array range
            index = characterList.Length - 1;

        // Toggle on the new model
        characterList[index].SetActive(true);
    }

    public void toggleRight() {
        // Toggle off the current model
        characterList[index].SetActive(false);

        index++;
        if (index == characterList.Length)
            index = 0;

        // Toggle on the new model
        characterList[index].SetActive(true);
    }

    public void selectButton() {
        // Save our 'character' selection
        PlayerPrefs.SetInt("characterSelected", index);
        SceneManager.LoadScene("scene_GOTO");
    }
}

If I understand your question + code… only 1 object is active at a time, and it can rotate.
So the other ones don’t really need to rotate unless you see them.
If that’s the case, save the rotation when it’s updated on the active one, and when the active selection changes, apply the rotation as soon as you make it active. :slight_smile:

Hmm, I think I found an easier way ! :wink:

I just used the parent object instead and applied the rotation there, that way it’s propagated to all the children.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class characterSelection : MonoBehaviour {

    [Header("Character List Parent Reference")]
    public GameObject characterListParent;

    private GameObject[] characterList;
    private int index;

    [Header("Turn Table Properties")]
    public bool turnTable = true;

    public bool rotateX = false;
    public bool rotateY = false;
    public bool rotateZ = false;

    public float rotationSpeed = 25.0f;

    private void Start() {
        index = PlayerPrefs.GetInt("characterSelected");

        characterList = new GameObject[transform.childCount];

        // Fill the array with our models ( all children of parent )
        for (int i = 0; i < transform.childCount; i++)
            characterList[i] = transform.GetChild(i).gameObject;

        // Toggle off their renderer
        foreach (GameObject go in characterList)
            go.SetActive(false);

        // Toggle on the selected character
        if (characterList[index])
            characterList[index].SetActive(true);
    }

    private void Update () {
        if (turnTable == true) {
            if(rotateX) {
                characterListParent.transform.Rotate (rotationSpeed * Time.deltaTime,0,0);
            }
            if(rotateY) {
                characterListParent.transform.Rotate (0,rotationSpeed * Time.deltaTime,0);
            }
            if(rotateZ) {
                characterListParent.transform.Rotate (0,0, rotationSpeed * Time.deltaTime);
            }
        }
    }

    public void toggleLeft() {
        // Toggle off the current model
        characterList[index].SetActive(false);

        index--; // Alternatives : index -=1; index = index - 1;
        if (index < 0)
            // Make sure we are not out of array range
            index = characterList.Length - 1;

        // Toggle on the new model
        characterList[index].SetActive(true);
    }

    public void toggleRight() {
        // Toggle off the current model
        characterList[index].SetActive(false);

        index++;
        if (index == characterList.Length)
            index = 0;

        // Toggle on the new model
        characterList[index].SetActive(true);
    }

    public void selectButton() {
        // Save our 'character' selection
        PlayerPrefs.SetInt("characterSelected", index);
        SceneManager.LoadScene("scene_GOTO");
    }
}

Cool, that’s good, too :slight_smile:

1 Like