in my game you fly a ship and need to avoid hitting objects
ive set up objects to move towards the ship but what i want to make is a sphere trigger on the ship that activates the object movement so they dont start moving towards the ship as the game starts but rather when it triggers the sphere
void OnTrigger enter () dosent work i think because the code needs to be in void update
anyways heres the code:
using UnityEngine;
using System.Collections;
public class ObjectAi : MonoBehaviour {
public Transform target;
public int objectSpeed;
public Transform Ship;
public int rotationSpeed;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Ship");
target = go.transform;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.transform.position, myTransform.position);
//look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
// move towards target
myTransform.position += myTransform.forward * objectSpeed * Time.deltaTime;
}
void OnTriggerEnter(Collider other)
{
}
}