Hi community, thanks for the space in the first place.
I’m simply trying to create a system where I add GameObjects to a public array of GameObjects so I can create a movement circuit with all their positions. Its Called Circuit Builder.
I have an array of Game objects from which I create an array of Vector2 with all the gameObjects positions.
Then I initialize each of the gameObjects via a script called “ObjectInCircuitMovementController” so they can start moving.
I know I must change the order of the values in the array so each gameObject starts at its own spot and follow the circuit from his position.
Everything is working fine, the objects seem to be Initializing with the correct Vectors in the correct order, but when they start moving they all have the same order of elements in the array so they all go first to the same spot and then start moving.
I did breakpoints and debugs, they all say that each object is receiving the info correctly, but on the first Update, they all go back to the same order. Any ideas? Thanks.
public class ObjectInCircuitMovementController : MonoBehaviour {
public bool move;
private Vector2[] targets;
private float moveSpeed;
private float timeToWait;
private bool showedTargets;
int i = 0;
// Update is called once per frame
void Update () {
if (move)
if (showedTargets == false)
{
Debug.Log(gameObject.name + "has the following Targets" + targets[0] + ", " + targets[1] + ", " + targets[2] + ", " + targets[3]);
showedTargets = true;
}
{
transform.position = Vector2.MoveTowards(transform.position, targets_, moveSpeed * Time.deltaTime);_
ControlMovement();
}
- }*
private void ControlMovement()
{
if (transform.position.x == targets_.x && transform.position.y == targets*.y)
{*_
i++;
if (i == targets.Length)
{
i = 0;
}
}
}
public void InitializeCyclicMovements(Vector2[] incomingTargets, float _moveSpeed, float _timeToWait)
{
string name = gameObject.name;
moveSpeed = _moveSpeed;
timeToWait = _timeToWait;
targets = incomingTargets;
move = true;
Debug.Log(gameObject.name + “has the following Targets” + incomingTargets[0] + ", " + incomingTargets[1] + ", " + incomingTargets[2] + ", " + incomingTargets[3]);
}
private IEnumerator WaitForMovement()
{
yield return new WaitForSeconds(timeToWait);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CircuitBuilder : MonoBehaviour {
public GameObject[] objectsToRotate;
public float _moveSpeed;
public float _timeToWait;
protected Vector2[] objectPositions;
// Use this for initialization
void Start() {
CheckParameters();
SetStartPositionsInCircuit();
InitializeMovementInEachObject();
}
private void InitializeMovementInEachObject()
{
for (int i = 0; i<objectsToRotate.Length; i++)
{
ObjectInCircuitMovementController cmController = objectsToRotate*.GetComponent();*
cmController.InitializeCyclicMovements(objectPositions, _moveSpeed, _timeToWait);
ChangeObjectsPositionsOrderInArray();
}
}
private void SetStartPositionsInCircuit()
{
objectPositions = new Vector2[objectsToRotate.Length];
for (int i = 0; i < objectsToRotate.Length; i++)
{
Transform actualObject = objectsToRotate*.GetComponent();*
objectPositions = actualObject.transform.position;
}
}
private void CheckParameters()
{
if (objectsToRotate.Equals(default(GameObject[])))
{
Debug.LogError(“Platform Rotator named: " + gameObject.name + " needs Objects to Start”);
return;
}
if (_moveSpeed.Equals(default(float)))
{
Debug.LogError(“Platform Rotator named: " + gameObject.name + " needs a moveSpeed to Start”);
return;
}
}
private void ChangeObjectsPositionsOrderInArray()
{
Vector2 savedPosition = objectPositions[0];
for (int i = 0; i<objectPositions.Length; i++)
{
int j = i + 1;
if (j < objectPositions.Length)
{
objectPositions = objectPositions[j];
}
else if (j >= objectPositions.Length)
{
objectPositions = savedPosition;
}
}
}
}