- Home /
Problem using Arrayed Transforms in IEnumerator
Hi everyone,
I'm trying to write a script, that can be applied to an object, in my case a machine, that will make all the 3D parts it consists of (not the unity components) "explode" and then float in the air once you enter the trigger Collider.
The purpose is to have a better overview of the machineparts in VR and later be able to view details.
What I have in mind, should look similar to this:
For the movement of the component to the triggered position, I want to use an IEnumerator
that changes the Transform[]
's position based on a Vector3.Lerp
. To make it possible to add a variable number of components, I want to array the Transforms for this.
I have no real idea, why this Array in the IEnumerator
doesnt work.
Here is my code for reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Explode : MonoBehaviour {
public float time;
public Transform[] stopTransform;
public Transform[] startTransform;
private Vector3[] startPosition;
private Vector3[] stopPosition;
// Use this for initialization
void Start () {
startPosition = new Vector3[startTransform.Length];
stopPosition = new Vector3[startTransform.Length];
for(int i=0; i < (stopTransform.Length - 1); i++){
if (stopTransform[i] != null) {
if(startTransform[i] != null && stopPosition[i] != null){
startPosition[i] = startTransform [i].position;
stopPosition[i] = stopTransform [i].position;
}
}
else {
this.enabled = false;
}
}
}
void OnTriggerEnter( Collider other ){
for (int i = 0; i < stopTransform.Length; i++) {
StartCoroutine (Expand (i));
}
}
IEnumerator Expand (int p) {
float t = 0.0f;
while (t < 1.0f) {
t += Time.deltaTime / time;
startTransform [p].position = Vector3.Lerp (startPosition [p], stopPosition [p], Mathf.SmoothStep (0.0f, 1.0f, t));
yield return null;
}
}
}
Your answer
Follow this Question
Related Questions
Getting radius around multiple transform 1 Answer
Can someone give me an example on looping through arrays? 0 Answers
Nested Prefab Issue 0 Answers
Bullet Hell : Shotgun spread and locking it to one axis 0 Answers
Undeclared transform array 0 Answers