Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy attack/follow Player script?

$
0
0
Trying to write a script to get a skeleton (an enemy) to follow my character and attack when close enough by punching/swiping. I have tried adding colliders to the skeleton's arms and causing them to take health away from the player, but the main issue is to get the skeletons to move with the correct animations triggering at the right times. At the moment I also have a collider on the player of course, and a collider around the player's sword. Whenever the collider of the player or sword come into contact, however, the skeleton slides away as if on an icy surface... Did I do something wrong in the script for the enemy? This is what I have at current (Errors about the 'get_time' are also showing in the console window but I can't fix them).
#pragma strict

var target : Transform;
var moveSpeed = 2.5;
var rotationSpeed = 4; 
var attackThreshold = 3;
var chaseThreshold = 15;
var giveUpThreshold = 25;
var attackRepeatTime = 1;
 
private var chasing = false;
private var attackTime = Time.time;
 
var myTransform : Transform;
 
function Awake()
{
    myTransform = transform;
}
 
function Start()
{
     target = GameObject.FindWithTag("Player").transform;
}
 
function Update () {
 
    
    var distance = (target.position - myTransform.position).magnitude;
 
    if (chasing) {
 
        
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
        Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
        
        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
        
        if (distance > giveUpThreshold) {
            chasing = false;
        }
        if (distance < attackThreshold && Time.time > attackTime) {
            animation.Play("attack");
            attackTime = Time.time + attackRepeatTime;
        }
    } else {
        if (distance < chaseThreshold) {
            chasing = true;
            animation.Play("run");
        }
    }
}

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>