Hi lads, I’m new to unity. I have a game object to simulate the enemy and set main camera to Target:Transform. The enemy code is:
#pragma strict
var distance : float;
var Target : Transform ;
var detectRange : float = 25;
var attackRange : float = 5;
var movementSpeed : float = 5;
var damp : float = 4;
var bloodOut : GameObject;
function Start () {
}
function Update () {
distance = Vector3.Distance(Target.position, transform.position);
if(distance < detectRange && distance > attackRange){
renderer.material.color = Color.yellow;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.position - transform.position), Time.deltaTime * damp);
transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
}
if(distance > detectRange){
renderer.material.color = Color.green;
}
if(distance < attackRange){
renderer.material.color = Color.red;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.position - transform.position), Time.deltaTime * damp);
}
}
This works good. Then I made this enemy game object as a prefab and the code doesn’t work. The Target is none and I can’t drag camera in. Could someone tell me why and how to fix it?
But if I drag the prefab into map and drag camera into Target then it’s working.