void OnShoot() { RaycastHit hit; if (Physics.Raycast(fpsCamera.transform.position, fpsCamera.transform.forward, out hit)) { Destructable destruct = hit.transform.GetComponent <Destructable>(); TargetBehaviour target = hit.transform.GetComponent <TargetBehaviour>(); if (target != null) { target.TakeDamage(); } else if (destruct != null) { destruct.TakeDamage(); } else { GameObject shoot = Instantiate(shootingEffect, hit.point, Quaternion.LookRotation(hit.normal)); Destroy(shoot, 0.1f); } if (hit.rigidbody != null) { hit.rigidbody.AddForce(hit.normal * addForce); } } }
public void CastRay() { muzzleSmoke.Play(); RaycastHit hit; if (Physics.Raycast(gunnerCam.transform.position, gunnerCam.transform.forward, out hit, range)) { if (Debugging) { Debug.Log(hit.transform.name); } TargetBehaviour target = hit.transform.GetComponent <TargetBehaviour>(); if (target != null) { target.TakeDamage(damage); } if (hit.rigidbody != null) { hit.rigidbody.AddForce(hit.normal * impactForce); } GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal)); Destroy(impactGO, 2f); } }
void Update() { if (info.IsAutomatic) { if (Input.GetButton("Fire1")) { RaycastHit hit; bool hitSuccess; (hitSuccess, hit) = controller.Shoot(); if (hitSuccess) { if (hit.transform.CompareTag("Target")) { TargetBehaviour target = hit.transform.GetComponent <TargetBehaviour>(); target.TakeDamage(Convert.ToSingle(info.Damage)); } } } } }