Animation not consistent
Hello all,
I'm trying to debug a (probably) very simple problem, but I think I'm missing something. The problem is as follows: I have my main character with a few takedown animations (the ones by 3d-brothers in the asset store). My script activates the animation with a trigger and then moves the victim to a given location. But the weird thing is that sometimes my main character is inside the victim, sometimes it works perfect and sometimes the character does the takedown far away from the victim. I have no idea how to solve it. I have the collider of the victim deactivated and the rotation of the character disabled at the time of the attack. Here's the relevant part of my character script:
[HideInInspector]
public bool attackFinished;
[System.Serializable]
public struct Attack {
public string name;
public Transform position;
}
public Attack[] attacks;
public AIScript victim;
public CameraMovement cameraMovement;
void Update(){
if (Input.GetKeyDown(KeyCode.F)) {
float smallestDistance = Mathf.Infinity;
AIScript closestEnemy = null;
AIScript[] allEnemies = FindObjectsOfType<AIScript>();
print(allEnemies.Length);
for (int i = 0; i < allEnemies.Length; i++) {
float distance = Vector3.Distance(allEnemies[i].transform.position, transform.position);
if (distance < 1 && distance < smallestDistance) {
closestEnemy = allEnemies[i];
smallestDistance = distance;
}
}
if (closestEnemy != null) {
float angle = Vector3.Angle(closestEnemy.transform.forward, transform.position - closestEnemy.transform.position);
if (angle > 100) {
StartCoroutine(AttackStealth(closestEnemy, 0));
} else if (angle < 45) {
StartCoroutine(AttackStealth(closestEnemy, 1));
}
}
}
}
IEnumerator AttackStealth(AIScript victim, int attack) {
canMove = false;
cameraMovement.canRotate = false;
animator.SetLayerWeight(2, 1);
animator.SetTrigger(attacks[attack].name);
victim.GetAttacked(attacks[attack].name, attacks[attack].position.position, attacks[attack].position.rotation);
attackFinished = false;
do {
yield return null;
} while (!attackFinished);
animator.SetLayerWeight(2, 0);
cameraMovement.canRotate = true;
canMove = true;
yield break;
}
And the part of my AIScript:
public void GetAttacked(string triggerName, Vector3 pos, Quaternion rot) {
character.BeginAnimation(triggerName);
pos.y = transform.position.y;
transform.position = pos;
transform.rotation = rot;
Destroy(character);
Destroy(agent);
Destroy(GetComponent<CapsuleCollider>());
Destroy(GetComponent<Rigidbody>());
Destroy(this);
}
the attackFinished bool is controlled by a mecamin script and goes to true when the takedown is finished.
I'm sure I'm missing something stupid, but I can't find what.
P.S. My character hierarchy is as follows:
The character moves with root motion.
Thanks in advance and kind regards,
Rens