// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { if (Physics.Raycast(new Ray(cam.transform.position + cam.transform.forward * .5f, cam.transform.forward), out hit, 20)) { DisparoGo.SetActive(true); ExplosionGo.transform.position = hit.point; ExplosionGo.SetActive(true); Invoke("ApagarEfecto", 2); print(hit.collider.name); } Destroyer health = hit.collider.GetComponent <Destroyer>(); if (health != null) { health.Damage(gunDamage); } if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * hitForce); } } }
// Update is called once per frame void Update() { if (Input.GetButtonDown("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; StartCoroutine(ShotEffect()); Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0)); RaycastHit hit; laserline.SetPosition(0, gunEnd.position); if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange)) { laserline.SetPosition(1, hit.point); Destroyer health = hit.collider.GetComponent <Destroyer>(); if (health != null) { health.Damage(gunDamage); } if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * hitForce); } } else { laserline.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange)); } } }