Home > Enterprise >  rotate an instantiated object from an array
rotate an instantiated object from an array

Time:08-22

I have an array which I am cycling through using my arrow keys, my goal is to make the objects rotate when they are instantiated (including the first) and then when I click them using a mouse it will stop them from rotating. Unfortunately I cant seem to find a good way to get the rotation to affect my instantiated objects.

I havent written the on mouse click aspect yet, but I was planning on just using a raycast.

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

public class ObjectSelector : MonoBehaviour
{
// This array stores the Prefabs
public GameObject[] ObjectArray;

// Integer for stepping through array Prefabs
private int selectedObject = 0;

// Location to instantiate the prefab
public Transform locator;

// Holds the currently instantiated Prefab in the scene
public GameObject tempMesh;

void Start()
{
    // Instantiate initial Prefab
    tempMesh = Instantiate(ObjectArray[selectedObject], locator.position, locator.rotation);
}



void Update()
{
    if (Input.GetKeyDown(KeyCode.RightArrow))
    {

        // Step to next position in array
        selectedObject  ;

        // If Array count excedes Array index then go back to first position in array
        if (selectedObject >= ObjectArray.Length)
        {
            selectedObject = 0;
        }

        // Remove previously instantiated prefab from hierarchy
        Destroy(tempMesh);

        // Instantiated next Prefab
        tempMesh = Instantiate(ObjectArray[selectedObject], locator.position, locator.rotation);

        // Print Counter for debug
        print(selectedObject);


    }
    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {

        // Step to next position in array
        selectedObject--;

        // If Array count excedes Array index then go back to first position in array
        if (selectedObject >= ObjectArray.Length)
        {
            selectedObject = 5;
        }
        if (selectedObject < 0)
        {
            selectedObject = 5;
        }

        // Remove previously instantiated prefab from hierarchy
        Destroy(tempMesh);

        // Instantiated next Prefab
        tempMesh = Instantiate (ObjectArray [selectedObject], locator.position, locator.rotation);

        // Print Counter for debug
        print(selectedObject);


    }
    GameObject yourObject = Instantiate (ObjectArray [Capsule], new Vector3 (0, 0, 3.0f), Quaternion.identity);

    // Rotate the object around its local X axis at 1 degree per second
    yourObject.transform.Rotate(Vector3.right * Time.deltaTime);

}

}

CodePudding user response:

Remember that Unity uses a Component model.

The easiest way to affect a GameObject is to attach a script to it. Try to attach a separate 'rotate until clicked' script to the objects you are instantiating.

For the click detection a ray-cast is a good solution.

EDIT: with the further information from comments, this was tried but failed in an unusual way (the rotation was progressive). I believe this is still the correct way to achieve the desired effect, but without the previous attempt's code I can't correct it.

The simple fix to the code as posted would be:

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

public class ObjectSelector : MonoBehaviour
{
// This array stores the Prefabs
public GameObject[] ObjectArray;

// Integer for stepping through array Prefabs
private int selectedObjectIndex = 0;

// Location to instantiate the prefab
public Transform locator;

// Holds the currently instantiated Prefab in the scene
public GameObject selectedObject;

void Start()
{
    // Instantiate initial Prefab
    selectedObject = Instantiate(ObjectArray[selectedObjectIndex], locator.position, locator.rotation);
}



void Update()
{
    bool objectChanged = false;
    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        // Step to next position in array
        selectedObjectIndex  ;

        // If Array count exceeds Array index then go back to first position in array
        if (selectedObjectIndex >= ObjectArray.Length)
        {
            selectedObjectIndex= 0;
        }
    
        objectChanged = true;
    }
    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {

        // Step to next position in array
        selectedObjectIndex--;
        // Wrap to end of list if needed
        if (selectedObjectIndex< 0)
        {
            selectedObjectIndex= ObjectArray.Length - 1;
        }
    
        objectChanged = true;
    }

    if (objectChanged)
    {
        // Remove previously instantiated prefab from hierarchy
        Destroy(selectedObject);

        // Instantiate next Prefab
        selectedObject= Instantiate(ObjectArray[selectedObjectIndex], locator.position, locator.rotation);

        // Print Counter for debug
        print(selectedObjectIndex);
    }

    // Rotate the object around its local X axis at 1 degree per second
    selectedObject.transform.Rotate(Vector3.right * Time.deltaTime);

}
  • Related