Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Aiming in and Aiming out animation issue

Discussion in 'Animation' started by connectrohit47, Jan 2, 2024.

  1. connectrohit47

    connectrohit47

    Joined:
    Jan 2, 2024
    Posts:
    1
    The issue is that i have made two animations for a gun aiming in and aiming out, aiming out is properly running but aiming in is continuously running.What's the issue i can't able to find? Below is the code used :
    using System.Collections;
    using System.Collections.Generic;
    using Unity.Collections.LowLevel.Unsafe;
    using UnityEngine;
    public class RayCastSmgFire : MonoBehaviour
    {
    public Animator animator;
    public Camera player;
    public float FireRate = 10f;
    private float timeBetweenNextShot;
    public MouseMovement mouse;
    [Header("Recoil")]
    public float vRecoil = 1f;
    public float hRecoil = 1f;
    public Animator aimAnim;
    public bool aim;
    [Header("Ammo Management")]
    public int ammocount = 25;
    public int availableammo = 100;
    public int maxAmmo = 25;

    void Update()
    {
    if (Input.GetButtonDown("Fire2"))
    {
    aim =!aim;
    }
    if (aim == true)
    {
    aimAnim.SetBool("Aim", true);
    }
    else
    {
    aimAnim.SetBool("Aim", false);
    }
    if (Input.GetKeyDown(KeyCode.R) && ammocount <= maxAmmo)
    {
    mouse.AddRecoil(0, 0);
    animator.SetBool("Reload", true);
    animator.SetBool("Shoot", false);
    }
    if (ammocount <= 0)
    {
    mouse.AddRecoil(0, 0);
    animator.SetBool("Reload", true);
    animator.SetBool("Shoot", false);
    return;
    }
    if (Input.GetButton("Fire1") && Time.time >= timeBetweenNextShot)
    {
    timeBetweenNextShot = Time.time + 1f / FireRate;
    float h = Random.Range(-hRecoil, hRecoil);
    animator.SetBool("Shoot", true);
    mouse.AddRecoil(vRecoil, h);
    weapon();
    }
    if (Input.GetButtonUp("Fire1"))
    {
    mouse.AddRecoil(0, 0);
    animator.SetBool("Shoot", false);
    }
    }
    void weapon()
    {
    ammocount--;
    RaycastHit hit;
    if (Physics.Raycast(player.transform.position, player.transform.forward, out hit))
    {
    Debug.Log(hit.transform.name);
    }
    }
    public void Reload()
    {
    availableammo = availableammo - maxAmmo + ammocount;
    ammocount = maxAmmo;
    animator.SetBool("Reload", false);
    }
    }

    Animator :
    Aimout:
    upload_2024-1-2_15-9-0.png

    upload_2024-1-2_15-9-19.png

    Aimin:
    upload_2024-1-2_15-9-42.png
     

    Attached Files: