Hey there! I’ve been working in a script that’s intended to run in a for loop.
While I’ve done a script that runs for a single gameObject, I can’t make it to work for it to run in many gameObjects while mantaining an organized way.
Below is the working script, working for a single gameObject.
using UnityEngine;
using System.Collections;
public class randomInit : MonoBehaviour
{
public GameObject prefab2;
public GameObject currentPos;
public float speed = 10f;
public Transform pointA;
public Transform pointB;
public bool moving = true;
IEnumerator waitForAChange()
{
float y = Random.Range(5f,10f);
yield return new WaitForSeconds(y);
moving = false;
}
IEnumerator waitForAChange2()
{
float y = Random.Range(5f,10f);
yield return new WaitForSeconds(y);
moving = true;
}
void changePosition()
{
if(moving)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.Lerp(transform.position,pointA.position,step);
StartCoroutine(waitForAChange());
}
if(!moving)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.Lerp(transform.position,pointB.position,step);
StartCoroutine(waitForAChange2());
}
}
void Update()
{
changePosition();
Destroy(GameObject.FindGameObjectWithTag("Respawn"),15f);
//prefab = GameObject.FindGameObjectsWithTag("Respawn");
//Destroy(prefab,2f);
}
}
Then, I’ve tried to make another script that runs in a for loop, and still stays organized. What I want is that for each ‘public GameObject toMove;’ to move from point01 to point02 and viceversa, all of this inside the class MovingOstacles.
Here’s the code:
using UnityEngine;
using System.Collections;
public class arrayOfarray : MonoBehaviour
{
/*changes the position of certain obstacles*/
[System.Serializable]
public class movingObstacles
{
public GameObject toMove;
public Transform point01;
public Transform point02;
public bool Moving = false;
public float speedMove;
//public GameObject prefabObstacle;
}
public movingObstacles[] onMove;
IEnumerator waitForAChange()
{
var equis2 = new movingObstacles();
float y = Random.Range(5f,10f);
yield return new WaitForSeconds(y);
Debug.Log("seconds : " + y);
equis2.Moving = false;
}
IEnumerator waitForAChange2()
{
var equis3 = new movingObstacles();
float y = Random.Range(5f,10f);
yield return new WaitForSeconds(y);
Debug.Log("seconds : " + y);
equis3.Moving = true;
}
void changePosition()
{
var equis = new movingObstacles();
for(int i = 0; i < onMove.Length; i++)
{
if(equis.Moving)
{
equis.Moving = true;
float step = equis.speedMove * Time.deltaTime;
equis.toMove.transform.position = Vector3.Lerp(transform.position,equis.point01.position,step);
StartCoroutine(waitForAChange());
Debug.Log("moving");
}
if(!equis.Moving)
{
equis.Moving = false;
float step = equis.speedMove * Time.deltaTime;
transform.position = Vector3.Lerp(transform.position,equis.point02.position,step);
StartCoroutine(waitForAChange2());
Debug.Log("not moving");
}
}
}
/*end of the snippet*/
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
changePosition();
}
}
And below is the screenshot that shows how each Element in the array has 5 more elements. I want the element ToMove to move between the elements point01 and point02 with a certain speed.
Any thoughs?
ps- not a native english speaker, sorry for any mistakes.