Hi Xan You help with this OnTriggerEnter script? It is supposed to move the camera forward when the soda hits the trigger.

Ok, so I have a script on the camera that is supposed to move the camera to the target when the soda hits the designated trigger, but it says “Array index is out of range” help!
Heres the code

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

public class Soda : MonoBehaviour {

public Transform[] target;
public float speed;
public GameObject cola;
public GameObject trigger;

private int current;

private void OnTriggerEnter(Collider col)
{

 if (cola == trigger)
    {
        if (transform.position != target[current].position)
        {
            Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * Time.deltaTime);
            GetComponent<Rigidbody>().MovePosition(pos);
        }
        else current = (current + 1) % target.Length;
    }
}

}

using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Soda : MonoBehaviour {
public Transform target;
public float speed;
public GameObject cola;
public GameObject trigger;
private int current;
public void OnTriggerEnter(Collider col)
{
if (col.transform == cola.transform)
{
if (transform.position != target[current].position)
{
Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * Time.deltaTime);
GetComponent().MovePosition(pos);
}
else current = (current + 1) % target.Length;
}
}
}