- Home /
I need my AI to attack any object who is tagged with a specific tag like let's say "enemies".Can someone help me?.I tryed everything and nothing seems to work.
var distance;
//pe cn urmarim?;
var target : Transform;
//unde dam si unde doare;
var script = "attributes";
var lookAtDistance = 15.0;
var attackRange = 10.0;
var moveSpeed = 5.0;
var damping = 6.0;
private var isItAttacking = false;
var attackNow : int;
//sa ne ataceee;
var dmg = 0.001;
var dmg1 :int;
var AttackZone=10;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.yellow;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
//animatia de atac cand este in aria vizuala destinata atacului;
if(distance < AttackZone){
animation.CrossFade("enemyAttack");
}
//atacul in sine
if(animation["enemyAttack"].enabled == true){
attackNow = 1;
}
else
{
attackNow = 0;
}
if(attackNow == 1){
target.GetComponent(script).stats["currhp"]=target.GetComponent(script).stats["currhp"] - dmg;
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
isItAttacking = true;
renderer.material.color = Color.red;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}
Answer by Griffo · Jul 26, 2012 at 12:28 PM
Something like this ..
Make sure your player has been tagged as Player, then drag it into the Target in the Inspector window
Hope this helps ..
#pragma strict
var Target : Transform; // Drag your player to attack in here
var distance = 30; // Distance
var lookAtDistance = 20; // Distance to keep looking at the player
var attackRange = 10; // Range to start the attack
var attackSpeed = 3.0; // Speed to attack player
var dampingLook = 6.0; // Slow the rotation
function Awake()
{
if(!Target)
{
Target = GameObject.FindWithTag("Player").transform;
}
}
function Update()
{
distance = Vector3.Distance(Target.position, transform.position);
if(canAtackTarget())
{
var targetRotation = Quaternion.LookRotation(Target.position - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * dampingLook);
}
if(canAtackTarget() && !attackRange){
attack();
}
}
function canAtackTarget()
{
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
print("Out of range");
return false;
}
if(distance < attackRange)
{
renderer.material.color = Color.red;
print("Attacking");
attack();
return true;
}
var seeYou : RaycastHit;
// Check to see if there is anything inbetween enemy and player
if(Physics.Linecast(transform.position, Target.position, seeYou))
{
if(seeYou.collider.gameObject.tag != "Player")
{
print("Can not see you " + seeYou.collider.gameObject.name + " in the way");
renderer.material.color = Color.green;
return false;
}
else
{
//Player ditected
renderer.material.color = Color.yellow;
return true;
}
}
return true;
}
function lookAt()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
}
function attack()
{
// The Vector3 0.2 stops the object going into the floor
transform.Translate(Vector3(0, 0.2, 1) * attackSpeed *Time.deltaTime);
}
Dude that's awesome sorry for the late replay, the modifications you made to my script are working like a charm.
Answer by Achilleterzo · Jul 26, 2012 at 11:06 AM
You can do it finding playerControll class in other target by using GetComponent() != null
Answer by perchik · Jul 24, 2012 at 06:32 PM
Just check to see if GameObject.tag = "Enemy" before attacking....
already tried that before i posted the question....thx anyway
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help with javascript AI 1 Answer
Enemy AI problems & Tree problems 0 Answers
How to Respawn Ai Enemies After Destroy Gameobject?? 4 Answers
Argument out of range. 1 Answer