using System.Collections;
using System.Security.Cryptography;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;
public class gunScript : MonoBehaviour
{
public float damage = 10f;
public float headShotDamage = 20f;
public float range = 300f;
public Camera cam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public Animator recoilAni;
private bool recoilDone = true;
public GameObject hatObject;
void Start()
{
hatObject = GameObject.Find("Cowboyenemy/CowboyRio_Unity/CowboyRIO_Hat");
}
void Update () {
if (Input.GetButtonDown("Fire1")) {
Shoot();
StartCoroutine(shotTimer());
}
}
void Shoot ()
{
if (recoilDone == true)
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range))
{
recoilAni = GetComponent<Animator>();
Debug.Log(hit.transform.name);
enemyScript target = hit.transform.GetComponent<enemyScript>();
if (target != null)
{
if (target != hatObject)
{
Debug.Log("Hit body!");
target.takeDamage(damage);
}
if (target == hatObject)
{
Debug.Log("Hit head!");
target.takeDamage(headShotDamage);
}
}
GameObject impactWork = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactWork, 2f);
}
}
}
IEnumerator playRecoil()
{
recoilDone = false;
muzzleFlash.Play();
recoilAni.Play("GunRecoil");
yield return new WaitForSeconds(0.3f);
recoilAni.Play("gunPause");
recoilDone = true;
}
IEnumerator shotTimer()
{
if (recoilDone == true)
{
StartCoroutine(playRecoil());
yield return new WaitForSeconds(0.3f);
}
}
}
trying to find the gameobject with
hatObject = GameObject.Find(âCowboyenemy/CowboyRio_Unity/CowboyRIO_Hatâ);
and trying to check if the gameobject is the hat with
if (target == hatObject)
{
Debug.Log(âHit head!â);
target.takeDamage(headShotDamage);
}