so I’m doing an artillery game, so where the shell hit near the tank I want it to be tracked (stop moving for 2 seconds) which it works fine, but the problem is it never moves again no matter how many attempts and codes I tried to assign the speed to 1f again it never works. here are the codes
the code on the shell which it hits and cause the tracking.
using UnityEngine;
using System.Collections;
public class Tracked : MonoBehaviour {
public float FarAreaEffectt;
private Transform explosion;
private Transform enemy;
public static bool wasTracked = false;
void Awake() {
explosion = transform;
}
void OnCollisionEnter () {
Collider[] colls = Physics.OverlapSphere (explosion.position, FarAreaEffectt);
foreach(Collider col in colls) {
if(col.CompareTag("Enemy")) {
float distance = Vector3.Distance (col.transform.position, explosion.position);
tracked (col.transform);
Debug.Log ("enemy tracked");
Invoke ("move", 2f);
}
}
}
void tracked(Transform enemy) {
enemymove em = enemy.GetComponent<enemymove> ();
em.speed = 0f;
}
void OnDrawGizmosSelected() {
Gizmos.color = Color.red;
Gizmos.DrawWireSphere (transform.position, FarAreaEffectt);
}
public void move(Transform enemy){
Debug.Log ("invoked");
enemymove e = enemy.GetComponent<enemymove> ();
e.speed = 1f;
}
}
and here is the enemyMovement secript
using UnityEngine;
public class enemymove : MonoBehaviour {
public float speed;
private Transform target;
private int wavepointIndex = 0;
void Start () {
target = WayPoints.points [0];
}
void Update() {
Vector3 dir = target.position - transform.position;
Quaternion rot = Quaternion.LookRotation (dir);
transform.rotation = Quaternion.Slerp (transform.rotation, rot, speed * Time.deltaTime);
transform.Translate (dir.normalized * speed * Time.deltaTime, Space.World);
if (Vector3.Distance (transform.position, target.position) <= 0.4f) {
GetNextWayPoint ();
}
}
void GetNextWayPoint() {
if (wavepointIndex >= WayPoints.points.Length - 1) {
speed = 0f;
return;
}
wavepointIndex++;
target = WayPoints.points [wavepointIndex];
}
}
so what I’m doing wrong?
is it because the shell get destroyed after the first time it get collides with the terrain and cause the tracking?
please help.