- Home /
Question by
ThePhantomGoat · Aug 09, 2018 at 11:37 PM ·
ontriggerentermove an object
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;
}
}
}
Comment
Answer by dan_wipf · Aug 10, 2018 at 05:22 AM
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<Rigidbody>().MovePosition(pos);
}
else current = (current + 1) % target.Length;
}
}
}
Your answer
Follow this Question
Related Questions
Moving a object when player enters trigger and play a sound 1 Answer
The problems of Collider .... 1 Answer
OnTriggerEnter(hit : Collider) and (other : Collider) 2 Answers
OnTriggerEnter - Melee Combat 1 Answer
Trigger Spawning? 1 Answer