I have some trouble with this:
var myDamage : int = 5; //or whatever var attackRange : float = 10; //whatever your sphereCollider's diameter is. function Attack(target : GameObject) { if (Vector3.Distance(target.transform.position, transform.position) < attackRange) { Move(target.transform.position); } else { var targetHealth : Health = (Health)target.GetComponent("Health") as Health; //Casting in unity javascript. hideous. is why I use C# instead. targetHealth.Damage(damage); } } function Move(targetLocation : Vector3) { //however your character moves. Physics? Simple Transform.Translate()? Too vague to suggest anything here. In any case, make it move towards targetLocation. } function DoNothing() { //stop movement. make it scratch it's butt. }
“When comes in range it attacks”:
Have a gameObject called, say, AwarenessArea, childed to Zoombie. Give AwarenessArea a SphereCollider component. Make SphereCollider’s diameter the same number you set attackRange to. make sure IsTrigger = true.
Make a script called something like ZoombieAwareness (ZoombieAI?) and attach it to the AwarenessArea gameObject.
Don’t know how you’ve identified the player gameObject. Assuming you’ve given it a tag named “Player”
var myActions : ZoombieActions; function Awake() { myActions = (ZoombieActions)GetComponent("ZoombieActions") as ZoombieActions } function OnTriggerEnter (thatThingIHit : Collider) { if ( thatThingIHit .CompareTag("Player")) myActions.Attack(thatThingIHit.gameObject); } function OnTriggerExit (thatThingIHit : Collider) { if ( thatThingIHit .CompareTag("Player")) myActions.DoNothing(); }