- Home /
Question by
lullaharsh · Sep 01, 2012 at 06:46 AM ·
playerenemy
Attack within 0.61 meters?
I have an enemy-follow script. The script is here
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform;
//cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
//target the player
}
function Update ()
{
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
I want to AI the enemy such that if it is 0.61 meters from me , it will fire. How can I do that?
Comment
You must use $$anonymous$$ETERS and $$anonymous$$ILOGRA$$anonymous$$S in Unity3D.
Best Answer
Answer by PProductions · Sep 01, 2012 at 01:54 PM
Simply add a Vector3.Distance check:
function Update () {
if(Vector3.Distance(transform.position, target.position) > 0.61) {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position),rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else {
//Attack Code Here
}
}