Hey, I need help with the animator transitions for my enemies in my game. I already hand made the animations with different sprites I just need help with the animator portion. This is what my animator looks like right now.
Each transition is on the bool of if shoot has been set or not. The enemy can shoot but I do not see it play the animation
void shoot ()
{
animator.SetBool( "Shoot", true );
Instantiate( Bullet, new Vector3 (bulletPosx, bulletPosy, 0), Quaternion.identity );
StartCoroutine( StopAnim() );
}
IEnumerator StopAnim ()
{
yield return new WaitForSeconds(1);
animator.SetBool( "Shoot", false );
}
Did you add the shoot condition to the transition between Enemy_Idle
and Enemy_Shoot
?
To do it select the transition arrow and add it in the inspector. Add a screenshot of the transition in the inspector window here if it still doesn’t work.
Otherwise also check if Shoot
is being set to true
in the animator window at runtime when shoot()
is called. If the condition is added and the bool
is changed, there is no way the transition doesn’t happen.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShoot : MonoBehaviour
{
public GameObject Bullet;
public Transform bulletPos;
public Animator animator;
public float bulletPosx;
public float bulletPosy;
private float timer;
private GameObject Player;
// Start is called before the first frame update
void Start()
{
if (gameObject != null){
Player = GameObject.FindGameObjectWithTag("Player");
animator = GameObject.FindGameObjectWithTag("EnemyGFX").GetComponent<Animator>();
}
}
// Update is called once per frame
void Update()
{
bulletPosx = bulletPos.position.x;
bulletPosy = bulletPos.position.y;
float distance = Vector2.Distance(transform.position, Player.transform.position);
if (distance < 20){
timer += Time.deltaTime;
if (timer > 1){
timer = 0;
shoot();
}
}
}
void shoot(){
animator.SetBool("Shoot", true);
Instantiate(Bullet, new Vector3 (bulletPosx, bulletPosy, 0), Quaternion.identity);
StartCoroutine(StopAnim());
}
IEnumerator StopAnim(){
yield return new WaitForSeconds(1);
animator.SetBool("Shoot", false);
}
}
The shot is fired but the animation does not play.
I figured it out.
I just will not reset the bool
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShoot : MonoBehaviour
{
public GameObject Bullet;
public Transform bulletPos;
public Animator animator;
public float bulletPosx;
public float bulletPosy;
private float timer;
private GameObject Player;
// Start is called before the first frame update
void Start()
{
if (gameObject != null){
Player = GameObject.FindGameObjectWithTag("Player");
animator = GameObject.FindGameObjectWithTag("EnemyGFX").GetComponentInChildren<Animator>();
}
}
// Update is called once per frame
void Update()
{
bulletPosx = bulletPos.position.x;
bulletPosy = bulletPos.position.y;
float distance = Vector2.Distance(transform.position, Player.transform.position);
if (distance < 20){
timer += Time.deltaTime;
if (timer > 1){
timer = 0;
shoot();
}
}
}
void shoot(){
animator.SetBool("Shoot", true);
Instantiate(Bullet, new Vector3 (bulletPosx, bulletPosy, 0), Quaternion.identity);
}
}