Hello,
I have a gameobject containing waypoints with a trigger collider inside, and an arrowobject. How can i use the the waypoints as target of the arrowobject and ontriggerenter pass to the next waypoint?
using UnityEngine;
using System.Collections;
public class MissionManager : MonoBehaviour {
public GameObject arrowObject;
public Transform[] Target;
private int CurrentTarget = 0;
void Awake (){
}
void Update (){
arrowObject.transform.LookAt( Target );
}
void OnTriggerEnter(Collider other){
}
void PointaAtTarget (bool next){
if (next){
CurrentTarget++;
}
}
}
Thanks in advance for any help
One line fix-
arrowObject.transform.LookAt(Target[CurrentTarget]);
Other than that, I would make some other changes, for style and/or safety-
I would make Target and CurrentTarget begin with lower-case letters, because they are members, not types
I would make arrowObject be a Transform, so that you don’t need to use the .transform lookup every time you need to change its direction.
I would make sure that currentTarget never overruns the limit of the target array, something like this-
//either
currentTarget++;
if(currentTarget >= target.Length)
{
currentTarget = 0;
// This makes the target reset if it goes over the maximum number of targets.
}
// or
currentTarget = Mathf.Clamp(currentTarget + 1, 0, target.Length - 1);
// This stops the current target from overrunning the array by clamping it at the max value.
I don’t know exactly what you’re trying to do, but this modified script now makes the object to which it’s attached arrowObject to point the current target, and when the object to which this script is attached enters the current target trigger, CurrentTarget is incremented. The increment only occurs if the trigger entered is attached to the current target - this prevents from multiple waypoint increment case the object enters the trigger more than once.
using UnityEngine;
using System.Collections;
public class MissionManager : MonoBehaviour {
public GameObject arrowObject;
public Transform[] Target;
private int CurrentTarget = 0;
void Update (){
arrowObject.transform.LookAt( Target[CurrentTarget] );
}
void OnTriggerEnter(Collider other){
if (other.transform == Target[CurrentTarget]){
CurrentTarget++;
}
}
}