So I tested out my test model, setup the Animator and the script.
Animator:
What I’m trying to do with my script is to trigger the character’s action if it’s position reached a certain point.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerBattleAttackManager : MonoBehaviour {
public GameObject playerToAttack;
public GameObject enemyToAttack;
public bool startAttack;
//Damage Systems
public PheneloDamageSystem pheneloDamageSys;
private GameObject battleControllerObject;
private BattleController battleController;
private CharacterPlate attackerStats;
private EnemyPlate enemyStats;
private HashIDs hash;
private Animator playerAnim;
private Animator enemyAnim;
void Awake()
{
pheneloDamageSys = (PheneloDamageSystem)gameObject.GetComponent("PheneloDamageSystem");
hash = GameObject.FindGameObjectWithTag(Constants.Tags.BattleController).GetComponent<HashIDs>();
}
void Update()
{
if(startAttack)
{
getStats();
StartCoroutine(approachTarget());
startAttack = false;
}
}
void getStats()
{
attackerStats = (CharacterPlate)playerToAttack.GetComponent("CharacterPlate");
enemyStats = (EnemyPlate)enemyToAttack.GetComponent("EnemyPlate");
playerAnim = playerToAttack.GetComponent<Animator>();
enemyAnim = enemyToAttack.GetComponent<Animator>();
}
//This is the main part where I'm trying to animate the character
IEnumerator approachTarget()
{
Debug.Log("Start moving");
//First look at the target
playerToAttack.transform.LookAt(enemyToAttack.transform);
//Start the running animation
playerAnim.SetFloat(hash.speedFloat, 1);
//Move the character's localPosition while running
while(Vector3.Distance(playerToAttack.transform.localPosition, enemyToAttack.transform.localPosition) > 10)
{
//If the character is close enough to the target, blend the animation into Idle
if(Vector3.Distance(playerToAttack.transform.localPosition, enemyToAttack.transform.localPosition) < 15)
{
playerAnim.SetFloat(hash.speedFloat, 0);
}
playerToAttack.transform.Translate(Vector3.forward * (Time.deltaTime / 2));
yield return null;
}
}
}
What happens is the running animation plays back after the idle animation played once. Can someone help me with this?
Any help would be much appreciated. ![]()
