I am currently working on implementing an animation for an enemy jump attack in a orthogonal 2D game, where the enemy will patrol between 4 patrol points, and after finding the player will lock on and chase until within attack range. At the correct distance, the attack animation is executed, but the enemy does not perform the transform the same way as he does in the raw animation recording.
This is the attack animation recording:
And this is the attack as it is executed in the game:
I have searched and tried just about everything I could think of, and need help figuring out how to get the enemy to lunge at the player the way he does in the animation. I have looked into root motion en/disabling, rigid body implementation, parent object transformations, all to no avail.
Currently the script for the enemy controller looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoboWolfControllerAI : MonoBehaviour {
public float speed;
public float awarenessRange;
public float chaseRange;
public float attackRange;
public int attackDamage;
public float attackDelay;
public Transform[] patrolPoints;
Animator anim;
int currentPatrolIndex;
float distanceToTarget;
bool facingRight = true;
bool isAttacking = false;
private float lastAttackTime;
Transform target;
Transform currentPatrolPoint;
// Use this for initialization
void Start () {
//Find target object that we want to attack, when necessary
target = GameObject.Find ("Base").transform;
anim = GetComponent<Animator> ();
//Set very high for testing when player not in scene
//distanceToTarget = 1000;
lastAttackTime = 0f;
//Initializations for the patrolPoints array
currentPatrolIndex = 0;
currentPatrolPoint = patrolPoints [currentPatrolIndex];
}
// Update is called once per frame
void Update () {
//Find distance to player
distanceToTarget = Vector3.Distance(transform.position, target.position);
//If in range to attack
if (distanceToTarget < attackRange) {
Attack ();
}
//If player is seen and attack not in range
if((distanceToTarget < awarenessRange) && (distanceToTarget > attackRange)) {
Debug.Log ("Squirrel!?");
Chase ();
}
//If target not within range
if (distanceToTarget > awarenessRange) {
Patrol ();
}
}
void Attack () {
//Find direction of the target
Vector3 targetDir = target.position - transform.position;
//Face the direction of target
if (targetDir.x < 0) {
if (facingRight) {
Flip ();
}
} else {
if (!facingRight){
Flip ();
}
}
//Ensure we aren't attacking constantly
if (Time.time > lastAttackTime + attackDelay){
Debug.Log ("Attack!");
//Play jump animation, move sprite according to attack animation
isAttacking = !isAttacking;
anim.SetBool ("isAttacking", isAttacking);
//Call hurt player script, pass it attackDamage variable
lastAttackTime = Time.time;
}
isAttacking = !isAttacking;
}
void Chase () {
//Find direction of the target
Vector3 targetDir = target.position - transform.position;
//Face the direction of target
if (targetDir.x < 0) {
if (facingRight) {
Flip ();
}
} else {
if (!facingRight){
Flip ();
}
}
//Then chase it
transform.Translate (targetDir.normalized * Time.deltaTime * speed);
}
void Patrol () {
//Runs when we have reached a patrol point within .1f
if (Vector3.Distance (transform.position, currentPatrolPoint.position) < .1f) {
//Look forward one index in array, is it the last item?
if (currentPatrolIndex + 1 < patrolPoints.Length) {
currentPatrolIndex++;
} else {
//Go back to first patrol point if it was the last item of patrolPoints array
currentPatrolIndex = 0;
}
currentPatrolPoint = patrolPoints [currentPatrolIndex];
}
//Find direction vector that points to the next patrol point
Vector3 patrolPointDir = currentPatrolPoint.position - transform.position;
//Turn to face currentPatrolPoint, looking at the X-value to find relative direction to sprite
//If next patrol point is to the left
if (patrolPointDir.x < 0) {
if (facingRight) {
Flip ();
}
}
else {
if (!facingRight){
Flip ();
}
}
//Begin moving towards the target patrolPoint
transform.Translate (patrolPointDir.normalized * Time.deltaTime * speed);
}
void Flip() {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
This is my first post and first crack at creating a game, so any insight would be helpful–especially if it turns out I am going about implementing enemy AI all wrong. If any more details are necessary I can provide them.