I’m having trouble with this whole “Transform/GameObject” thing. I don’t really understand how to implement without mixing those two up. For example I uses Transform to calculate distance between my player and the enemy. On the other hand my AutoTarget script uses GameObject to locate the closest enemy to target. And I get mixed up in my “var target”. I got stucked there. Would anyone help me with this?
PlayerAttack.js script
var target : Transform;
var damage : float = 10;
var myTransform : Transform;
var attackThreshold = 3;
var attackRepeatTime = 1;
var projectile : GameObject;
private var attackTime = Time.time;
function Awake()
{
myTransform = transform;
}
function Update ()
{
target = GetComponent(AutoTarget).target;
var distance = (target.position - myTransform.position).magnitude;
var hit:RaycastHit;
if(distance < attackThreshold && Time.time > attackTime)
{
Instantiate (projectile, transform.position, transform.rotation);
attackTime = Time.time + attackRepeatTime;
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
AutoTarget.js script
var target = closest;
var closest : GameObject;
var Speed : float;
var Turn : float;
function Update () {
target = closest;
var enemies : GameObject[];
enemies = GameObject.FindGameObjectsWithTag("Enemy");
var distance = Mathf.Infinity;
var position = transform.position;
for(var enemy : GameObject in enemies)
{
var diff = (enemy.transform.position - transform.position);
var curDistance = diff.sqrMagnitude;
if(curDistance < distance)
{
closest = enemy;
distance = curDistance;
}
}
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(closest.transform.position - transform.position), Turn*Time.deltaTime);
transform.position += transform.forward*Speed*Time.deltaTime;
return closest;
}