So, I’m making sort of a 90s-era Resident Evil-inspired game. I have a script for tank controls, and another script for shooting mechanics. I have a monster character who currently runs on three scripts: one for AI pathfinding to the player, one for death and health, and another for attacking the player.
Currently, in order for the character to fight monsters in the game, they have to hold down the right mouse button, then left click to shoot their gun, without moving out of place (like in the classic style). Everything works beautifully, the monster dies, its AI is good, the shooting works, but the big issue comes when the monster actually attacks the player.
Two animations play when the player is attacked–one animation on the player getting attacked, and the monster attacking, and both run out of the monster attacking script. This works perfectly fine when the player is not aiming their gun or shooting, but when they are aiming and shooting, it causes all sorts of problems. If the player is aiming at the monster when the monster attacks, when the animation of them getting attacked is over, the player can’t move unless they right click to aim again. And if the enemy attacks the player while in the middle of a shooting animation, then the animation of the player getting attacked doesn’t play at all, and they’re stuck in the “aiming” animation.
I know all this is very vague, so I posted a gif of what happens if the “monster attack” script activates in the middle of the shooting script’s shooting Coroutine. If anyone has any idea how to fix this problem, it would be greatly appreciated, because I’ve been tearing my hair out for two days trying to fix it.
Codes:
Code for the “monster attacking” script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VampireBite : MonoBehaviour
{
public GameObject vampire;
public GameObject Raquel;
public GameObject bloodSquirt;
public GameObject biteTrigger;
public AudioSource biteSound;
public AudioSource raquelGrunt;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
StartCoroutine (VampireBiteRaquel());
}
}
IEnumerator VampireBiteRaquel()
{
vampire.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;
Raquel.GetComponent<TankControls>().enabled = false;
Raquel.GetComponent<WeaponMechanics>().enabled = false;
raquelGrunt.Play();
vampire.GetComponent<Animator>().Play("Bite");
Raquel.GetComponent<Animator>().Play("GetBitten");
biteSound.Play();
yield return new WaitForSeconds (1f);
bloodSquirt.SetActive(true);
yield return new WaitForSeconds (0.5f);
bloodSquirt.SetActive(false);
yield return new WaitForSeconds (2.5f);
vampire.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = true;
Raquel.GetComponent<WeaponMechanics>().enabled = true;
Raquel.GetComponent<TankControls>().enabled = true;
vampire.GetComponent<Animator>().Play("Stalk");
}
}
And the script for the shooting mechanics:
public class WeaponMechanics : MonoBehaviour
{
public static bool isAiming = false;
public GameObject thePlayer;
public float horizontalMove;
public AudioSource pistolShot;
public bool isFiring = false;
public float TargetDistance;
public int DamageAmount = 5;
void Update()
{
if (Input.GetMouseButtonDown(1))
{
isAiming = true;
thePlayer.GetComponent<Animator>().Play("Aim");
AutoAim.aimingGun = true;
}
if (Input.GetMouseButtonUp(1))
{
StartCoroutine(ReturnToTank());
AutoAim.aimingGun = false;
thePlayer.GetComponent<Animator>().Play("LowerGun");
}
if (isAiming == true)
{
if (Input.GetButton("Horizontal"))
{
horizontalMove = Input.GetAxis("Horizontal") * Time.deltaTime * 150;
thePlayer.transform.Rotate(0, horizontalMove, 0);
}
if (Input.GetMouseButtonDown(0) && isFiring == false)
{
isFiring = true;
StartCoroutine(FiringWeapon());
}
}
}
IEnumerator FiringWeapon()
{
RaycastHit Shot;
isFiring = true;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.back), out Shot))
{
TargetDistance = Shot.distance;
Shot.transform.SendMessage ("DamageVampire", DamageAmount, SendMessageOptions.DontRequireReceiver);
}
pistolShot.Play();
thePlayer.GetComponent<Animator>().Play("Shoot");
yield return new WaitForSeconds(0.95f);
thePlayer.GetComponent<Animator>().Play("AimIdle");
isFiring = false;
}
IEnumerator ReturnToTank()
{
yield return new WaitForSeconds(0.40f);
isAiming = false;
}
}