Hello there!
I’m trying to make an FPS game inspired by Classic Doom, so the “player hand and gun” are just drawn pictures, and for the animations, I’m just switching between sprites so it looks like the player is shooting, walking and such.
I don’t use unity a lot because I’m a student and don’t have much time, but do it as a hobby, so I don’t know everything there is to know about it at all.
The problem : I have a few animations, walking, shooting, jumping, and so on (currently only walking and shooting). I want the animator to play “walking” animation when the player is walking, and the “shoot” animation when the player presses Mouse(0). I have a code for that, and it kinda works, but it’s really scuffed; sometimes the “shoot” animation doesn’t play but a shot is shot (?). Sometimes after the shooting animation, the walking animation takes a few seconds until it starts playing and such stuff.
I basically want the animations to play at the right time, without delays and so on.
I also don’t know much about transitions, I know how to use them, but not 100% in the right way.
This is my code:
void Update()
{
if (gunType == WeaponType.pistol)
{
WalkAnim();
}
}
void FixedUpdate()
{
if (gunType == WeaponType.pistol)
{
if (Input.GetMouseButtonDown(0) && !isShooting)
{
StartCoroutine(pistolShoot());
}
}
}
IEnumerator pistolShoot()
{
isShooting = true;
GetComponent<Animator>().Play(shootAnim);
GameObject.Find("Shotgun+2").GetComponent<AudioSource>().Play();
RaycastHit hit;
if (Physics.Raycast(GameObject.FindWithTag("MainCamera").transform.position, GameObject.FindWithTag("MainCamera").transform.forward, out hit, range))
{
float Randomizer = Random.Range(0f, 350f);
GameObject bulletHole = Instantiate(bulletHolePref[Random.Range(0, bulletHolePref.Length)], hit.point + hit.normal * 0.001f, Quaternion.LookRotation(hit.normal), hit.transform);
Destroy(bulletHole, bulletHoleTime);
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * damage, ForceMode.Impulse);
}
}
yield return new WaitForSeconds(shootAnimLength);
isShooting = false;
}
void WalkAnim()
{
if (PlayerMovement.isWalking && !isShooting)
{
GetComponent<Animator>().Play(walkAnim);
}
}
Thank you so much for the help in advance <3