My animations break when I spawn enemy
I am new to coding so bare with me.
I have a spider queen boss that spawns spiderlings when she reaches certain point in her health.
How ever when they spawn they somehow break the attack animations I have for my player. Please help me!
The code for player attack:
using UnityEngine;
using System.Collections;
public class Sword : MonoBehaviour {
static Animator anim;
public GameObject hitbox;
public int Sworddamage = 25;
public float sec = 0.5f;
HandS handS;
public float AttackCD;
public float delayBetweenAttacks = 1.5f;
public float stamDrain = 50;
public int SwingReQ;
public AudioSource WeaponSource;
public AudioClip WeaponSound;
void Start () {
anim = GetComponentInParent<Animator>();
WeaponSource = GetComponent<AudioSource>();
handS = GetComponentInParent<HandS>();
}
// Update is called once per frame
void Update () {
attack();
block();
}
public void attack()
{
if (Input.GetButtonDown("Fire1") && Time.time > AttackCD && handS.currentStam >= SwingReQ)
{
AttackCD = Time.time + delayBetweenAttacks;
anim.SetBool("IsAttacking", true);
hitbox.SetActive(true);
StartCoroutine(LateCall());
handS.LessStamina(30);
WeaponSource.PlayOneShot(WeaponSound);
Debug.Log("hit");
}
else
{
anim.SetBool("IsAttacking", false);
}
}
public void block()
{
if (Input.GetButtonDown("Fire2"))
{
anim.SetBool("IsBlocking", true);
}
else
{
anim.SetBool("IsBlocking", false);
}
}
IEnumerator LateCall()
{
yield return new WaitForSeconds(sec);
hitbox.SetActive(false);
}
}
The code for the queen:
using UnityEngine;
using System.Collections;
public class SpiderQueen: MonoBehaviour {
private NavMeshAgent myAgent;
private Animator myAnimator;
EnemyHealth eh;
public Transform target;
public GameObject Spiderling;
public bool chaseTarget = true;
public float stopingDistance;
public float delayBetweenAttacks = 1.5f;
private float attackCD;
public float SpawnSpiderling = 50;
public int AmountSpiderling = 0;
private float distanceFromTarget;
// Use this for initialization
void Start () {
myAgent = GetComponent<NavMeshAgent>();
myAnimator = GetComponent<Animator>();
myAgent.stoppingDistance = stopingDistance;
attackCD = Time.time;
eh = GetComponentInChildren<EnemyHealth>();
}
// Update is called once per frame
void Update () {
}
public void ChaseTarget()
{
distanceFromTarget = Vector3.Distance(target.position, transform.position);
if (distanceFromTarget >= stopingDistance * 1 && distanceFromTarget <= stopingDistance * 1.1)
{
Attack();
}
else if(distanceFromTarget >= stopingDistance)
{
chaseTarget = true;
}
else
{
AttackL();
chaseTarget = false;
}
if (chaseTarget)
{
myAgent.SetDestination(target.position);
myAnimator.SetBool("IsWalking", true);
}
else
{
myAnimator.SetBool("IsWalking", false);
}
if(eh.currentHealth <= SpawnSpiderling)
{
while(AmountSpiderling < 5)
{
Instantiate(Spiderling, transform.position, transform.rotation);
AmountSpiderling++;
}
}
}
void Attack()
{
if(Time.time > attackCD)
{
Debug.Log("Attack");
myAnimator.SetTrigger("Attack");
attackCD = Time.time + delayBetweenAttacks;
}
}
void AttackL()
{
if(Time.time > attackCD)
{
myAnimator.SetTrigger("AttackL");
attackCD = Time.time + delayBetweenAttacks;
}
}
void AttackR()
{
if(Time.time > attackCD)
{
myAnimator.SetTrigger("AttackR");
attackCD = Time.time + delayBetweenAttacks;
}
}
void StartAttack()
{
transform.RotateAround(target.position, Vector3.up, 20 * Time.deltaTime);
}
}
The code for the spiderling:
using UnityEngine;
using System.Collections;
public class Spiderling : MonoBehaviour {
private NavMeshAgent myAgent;
private Animator myAnimator;
public Transform target;
public bool chaseTarget = true;
public float stopingDistance;
public float delayBetweenAttacks = 1.5f;
private float attackCD;
private float distanceFromTarget;
void Awake()
{
target = GameObject.FindWithTag("Player").transform;
myAgent = GetComponent<NavMeshAgent>();
myAnimator = GetComponent<Animator>();
myAgent.stoppingDistance = stopingDistance;
attackCD = Time.time;
}// Use this for initialization
void Start()
{
}
void Update()
{
Chaseplayer();
}
private void Chaseplayer()
{
distanceFromTarget = Vector3.Distance(target.position, transform.position);
if (distanceFromTarget >= stopingDistance)
{
chaseTarget = true;
}
else if(distanceFromTarget <= stopingDistance)
{
Attack();
chaseTarget = false;
}
if (chaseTarget)
{
myAgent.SetDestination(target.position);
myAnimator.SetBool("IsWalking", true);
}
else
{
myAnimator.SetBool("IsWalking", false);
}
}
void Attack()
{
if (Time.time > attackCD)
{
Debug.Log("Attack");
myAnimator.SetTrigger("Attack");
attackCD = Time.time + delayBetweenAttacks;
}
}
void AttackL()
{
if (Time.time > attackCD)
{
myAnimator.SetTrigger("AttackL");
attackCD = Time.time + delayBetweenAttacks;
}
}
void AttackR()
{
if (Time.time > attackCD)
{
myAnimator.SetTrigger("AttackR");
attackCD = Time.time + delayBetweenAttacks;
}
}
}
Comment
Answer by UnityCSharpNoob · Jul 29, 2016 at 01:03 PM
Could it have something to do with my weapon switching code?
using UnityEngine;
using System.Collections;
public class SwitchWeapons : MonoBehaviour
{
static Animator anime;
public GameObject Sword;
public GameObject Axe;
public GameObject Magic;
public GameObject Model2;
// Use this for initialization
void Start()
{
Model2.SetActive(false);
anime = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate()
{
if (Input.GetButtonDown("1"))
{
Model2.SetActive(false);
Sword.SetActive(true);
Axe.SetActive(false);
Magic.SetActive(false);
anime.SetBool("IsBlocking", true);
}
if (Input.GetButtonDown("2"))
{
Axe.SetActive(true);
Sword.SetActive(false);
Magic.SetActive(false);
Model2.SetActive(false);
}
if (Input.GetButtonDown("3"))
{
Model2.SetActive(true);
Magic.SetActive(true);
Sword.SetActive(false);
Axe.SetActive(false);
}
}
}