I am on my first steps of learning how to code and also learning about the unity engine. So I am sure that I am doing something wrong here and probably it is in my code . These past three days I am surfing though the internet trying to figure out what I am doing wrong but I still haven’t found out what it is.
I am using a number of cylinders with the following script attached to it
using UnityEngine;
using System.Collections;
public class MoveCapsule : MonoBehaviour
{
public float moveSpeed = 4f;
public bool canBite = false;
public GameObject zombieClone;
public bool canBiteBite = false;
//public Transform sPT;
void Start ()
{
InvokeRepeating("Wandering", 1, 3f);
}
void Update ()
{
GameObject[] ZombieClones = (GameObject[])
GameObject.FindObjectsOfType(typeof(GameObject));
foreach (GameObject go in ZombieClones)
{
ZombieCloneS zC= go.GetComponent<ZombieCloneS>();
if(zC == null || zC == this)
{
continue;
}
}
if ((canBite && Input.GetKeyDown (KeyCode.E)) || canBiteBite)
{
GameObject zombieCloneC = (GameObject) Instantiate(zombieClone, transform.position, transform.rotation);
Destroy(gameObject);
}
if(!transform.Find("ZombieDetect").GetComponent<ZombieDetect>().zombieDetected)
{
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
}
else //if (transform.Find("ZombieDetect").GetComponent<ZombieDetect>().zombieDetected)
{
CancelInvoke("Wandering");
RunToSafety ();
}
}
public void Wandering()
{
transform.Rotate(0,180,0);
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Player")
{
canBite = true;
//Debug.Log("Player Entered");
}
else if (other.gameObject.tag == "Zombie")
{
canBiteBite = true;
}
}
void OnCollisionExit(Collision other)
{
canBite = false;
canBiteBite = false;
}
void RunToSafety()
{
GameObject[] safetyPoints = GameObject.FindGameObjectsWithTag ("Safety");
foreach (GameObject sp in safetyPoints)
{
transform.position = Vector3.MoveTowards (transform.position, sp.transform.position, moveSpeed * Time.deltaTime);
}
}
}
The problem I am having is that when RunToSafety function is executed and the object MoveTowards the target(sp.tranfrom.position) with the same moveSpeed, it actually moves faster than when transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime) is executing.
Apparently I am missing something technical here but I can’t seem to be able to find out what.