hi I followed this tutorial on youtube on making an object that moves towards its closest target.
on the video it shows the code working, so why is it not working for me?
on the console it says that that line 6, cannot convert GameObject to GameObject[ ].
it also has a problem with line 19
transform.rotation=Quaternion.Slerp(transform.rotation.Quaternion.LookRotation(closest.transform.position-transform.position).Turn*Time.deltaTime);
but I didn’t understand it. can someone take a look and explain whats wrong and why this code doesn’t work despite it working in the previous version
var Speed : float;
var Turn : float;
function Update (){
var targets: GameObject[] = GameObject.FindGameObjectWithTag("Enemy");
var closest: GameObject;
var ClosestDist = Mathf.Infinity;
for (Target in targets){
var dist = (transform.position - Target.transform.position).sqrMagnitude;
if(dist < ClosestDist){
ClosestDist = dist;
closest = Target;
}
}
transform.rotation=Quaternion.Slerp(transform.rotation.Quaternion.LookRotation(closest.transform.position-transform.position).Turn*Time.deltaTime);
transform.position+=transform.forward*Speed*Time.deltaTime;
}
function OnCollisionEnter(collision : Collision){
Destroy (gameObject);
}