- Home /
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");
}
}
}
https://www.youtube.com/watch?v=YYqzz1dy3Ak
link to a tutorial series watched this last year best thing i ever did i watched all 300 but the 1ST 5 is what your looking for its c#
I did a video series last year in uJS. Probably start at video 23
Answer by Harryliu · May 06, 2014 at 04:17 AM
I have a simple enemy AI. see if this helps. so it would stop moving if it is far away from your itlink text would be harless, and if you get closer it would start chasing you.
Wow, ok. This looks like overkill for my skeleton, but you think it will work effectively? I'll try it then (ins$$anonymous$$d of my above script). I'll tell you how it goes in a few $$anonymous$$utes.
Okay, using that script bombards me with error messages.
Answer by guillopuyol · May 06, 2014 at 04:17 AM
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.
Answer by $$anonymous$$ · Jun 30, 2018 at 04:01 PM
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 .
Answer by H3yPlaya · Jun 30, 2018 at 04:15 PM
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.
}
}
Your answer
Follow this Question
Related Questions
Apply Damage To Player On Collison With Specific Game Object 1 Answer
Deduct health on collision 2 Answers
Adding a counter? 1 Answer
Enemy animation help? 1 Answer
Enemy Health Problem 2 Answers