Enemy attack/follow Player script?

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");
        }
    }
}

I have a simple enemy AI. see if this helps. so it would stop moving if it is far away from your it[26209-enemyai.txt|26209] would be harless, and if you get closer it would start chasing you.

The sliding effect you mention might be the result of the colliders not being set up as triggers, so the player is in fact pushing the skeleton away. Also, I’m not sure about the setup of your game, but having colliders on the sword seems like overkill. How about setting it so that if the skeleton is looking towards the player ± a few degrees, and within range, then play the attack animation and deduct health accordingly.

Hello ,
( you have post that question in 2014 and i see him in 2018 Im not sure if you got the solution or not , so i deside to answer , maybe it will help the others )

I have tried your script and i see some errors messages , if your problem is the Errors messages , so i find a solution .
Just add new one of this mark } in the last of the script , to close the Update function and it will fixed like it happend to me .

I hop it helped .

I have my own game kinda like this, and I don’t use lookRotation, it’s inefficient. instead I use setDestination and a navmesh, like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerAttack : MonoBehaviour {
    
    public GameObject[] Enemys;
    public GameObject EnemyAttacking;
    public LayerMask groundLayer;
    public NavMeshAgent playerAgent;

    private float AttackTimer = 0.0f;

    public int AttackDamage = 30;
    public float AttackSpeed = 0.5f;
    public bool Melee = false;
    public float AttackRange = 5.0f;

    void Start()
    {
        if (Enemys == null)
        {
            Enemys = GameObject.FindGameObjectsWithTag("Enemy");
        }

        if (Melee)
        {
            AttackRange = 0;
        }
    }

    void Update () {
        this is a simple left click on an AI to attack it.
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                EnemyAttacking = hit.transform.gameObject;
            }
        }

        if (Input.GetMouseButtonDown(1))
        {
            EnemyAttacking = null;
            AttackTimer = 0;
        }

        if (EnemyAttacking != null)
        {
            float AIDistance = Vector3.Distance(gameObject.transform.position, EnemyAttacking.transform.position);

            if (Melee)
            {
                if (AIDistance > 1)
                {
                    Chase();
                    AttackTimer = 0;
                    playerAgent.stoppingDistance = 0;
                }
                else
                {
                    AttackTimer += 1 * Time.deltaTime;

                    if (AttackTimer >= AttackSpeed)
                    {
                        Attack();
                        AttackTimer = 0;
                        playerAgent.stoppingDistance = 1;
                    }
                }
            }
            else
            {
                if (AIDistance > AttackRange)
                {
                    Chase();
                    AttackTimer = 0;
                }
                else
                {
                    playerAgent.transform.LookAt(EnemyAttacking.transform.position);
                    playerAgent.stoppingDistance = AttackRange;
                    this is so when the player is in range, he stops but keeps looking at the AI to attack.

                    AttackTimer += 1 * Time.deltaTime;

                    if (AttackTimer >= AttackSpeed)
                    {
                        Attack();
                        AttackTimer = 0;
                    }
                }
            }
        }

        if (AttackTimer < AttackSpeed)
        {
            playerAgent.stoppingDistance = 0;
        }
    }

    void Chase()
    {
        playerAgent.SetDestination(EnemyAttacking.transform.position);
        this is where i tell the player to follow the AI when attacking it.
    }

    void Attack()
    {
        Debug.LogFormat("{0} has delt {1}HP to {2}.", gameObject.name, AttackDamage, EnemyAttacking);
        EnemyAttacking.SendMessage("TakeDamage", AttackDamage);
        this is a function that ia have in another script, SendMessage is how i call it.
    }
}