void Shoot() { muzzleFlash.Play(); bulletGraphic.Play(); animatorref.Play("Base Layer.rifleanimation", 0, 0f); shooterref.Play("Base Layer.shooteranim", 0, 0f); gunsound.Play(); RaycastHit hit; // The following function returns a boolean value so if the ray hits something the following if statement runs // The following function also shoots out a ray in the direction we want and within the range we want to set if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) //out hit = unity stores all the information about the shot object in this hit variable { //Debug.Log(hit.transform.tag); Target target = hit.transform.GetComponent <Target>(); npcdamage npc = hit.transform.GetComponent <npcdamage>(); if (target != null) //Checks if we found the 'Target' Component/Script in the object we're shooting { target.TakeDamage(damage); } if (npc != null) //Checks if we found the 'npcdamage' Component/Script in the object we're shooting { if (hit.collider.tag == "head") { npc.DoDamage(headdamage); } else { npc.DoDamage(damage); } } if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * shootforce); } GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal)); Destroy(impactGO, 0.2f); } }
public void Melee() { RaycastHit hit; if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) //out hit = unity stores all the information about the shot object in this hit variable { npcdamage npc = hit.transform.GetComponent <npcdamage>(); if (npc != null) //Checks if we found the 'npcdamage' Component/Script in the object we're shooting { npc.DoDamage(punchdamage); } if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * punchforce); } } }