private void Fire() { Transform cam = Camera.main.transform; mNextFire = Time.time + mFireRate; Vector3 rayOrigin = cam.position; mLaserLine.SetPosition(0, transform.up * -10f); RaycastHit hit; if (Physics.Raycast(rayOrigin, cam.forward, out hit, mFireRange)) { mLaserLine.SetPosition(1, hit.point); CubeBehaviorScript cubeCtr = hit.collider.GetComponent <CubeBehaviorScript>(); if (cubeCtr != null) { if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * mHitForce); cubeCtr.hit(mLaserDamage); score++; Debug.Log("hit a cube !!!"); } } } else { mLaserLine.SetPosition(1, cam.forward * mFireRange); } StartCoroutine(laserFX()); }
// Shot the Laser // Shot the Laser private void Fire() { // Get ARCamera Transform Transform cam = Camera.main.transform; // Define the time of the next fire mNextFire = Time.time + mFireRate; // Set the origin of the RayCast Vector3 rayOrigin = cam.position; // Set the origin position of the Laser Line // It will always 10 units down from the ARCamera // We adopted this logic for simplicity mLaserLine.SetPosition(0, transform.up * -10f); // Hold the Hit information RaycastHit hit; // Checks if the RayCast hit something if (Physics.Raycast(rayOrigin, cam.forward, out hit, mFireRange)) { scoreCount++; scoreText.text = "SCORE: " + scoreCount; // Set the end of the Laser Line to the object hit mLaserLine.SetPosition(1, hit.point); // Get the CubeBehavior script to apply damage to target CubeBehaviorScript cubeCtr = hit.collider.GetComponent <CubeBehaviorScript>(); if (cubeCtr != null) { if (hit.rigidbody != null) { // apply force to the target hit.rigidbody.AddForce(-hit.normal * mHitForce); // apply damage the target cubeCtr.Hit(mLaserDamage); source = GetComponent <AudioSource>(); source.PlayOneShot(shootSound, 1F); Instantiate(explosionPrefab, hit.point, Quaternion.identity); } } } else { // Set the enfo of the laser line to be forward the camera // using the Laser range mLaserLine.SetPosition(1, cam.forward * mFireRange); } source = GetComponent <AudioSource>(); source.PlayOneShot(laserSound, 1F); // Show the Laser using a Coroutine StartCoroutine(LaserFx()); }
// Shot the Laser private void Fire() { // Get ARCamera Transform Transform cam = Camera.main.transform; // Define the time of the next fire mNextFire = Time.time + mFireRate; // Set the origin of the RayCast Vector3 rayOrigin = cam.position; // Set the origin position of the Laser Line // It will always 10 units down from the ARCamera // We adopted this logic for simplicity mLaserLine.SetPosition(0, transform.up * -10f); // Hold the Hit information RaycastHit hit; // Checks if the RayCast hit something if (Physics.Raycast(rayOrigin, cam.forward, out hit, mFireRange)) { // Set the end of the Laser Line to the object hit mLaserLine.SetPosition(1, hit.point); } else { // Set the enfo of the laser line to be forward the camera // using the Laser range mLaserLine.SetPosition(1, cam.forward * mFireRange); } // Checks if the RayCast hit something if (Physics.Raycast(rayOrigin, cam.forward, out hit, mFireRange)) { // Set the end of the Laser Line to the object hitted mLaserLine.SetPosition(1, hit.point); // Get the CubeBehavior script to apply damage to target CubeBehaviorScript cubeCtr = hit.collider.GetComponent <CubeBehaviorScript>(); if (cubeCtr != null) { if (hit.rigidbody != null) { // apply force to the target hit.rigidbody.AddForce(-hit.normal * mHitForce); // apply damage the target cubeCtr.Hit(mLaserDamage); } } } StartCoroutine(LaserFx()); }
// Shot the Laser private void Fire() { // Get ARCamera Transform Transform cam = Camera.main.transform; // Define the time of the next fire mNextFire = Time.time + mFireRate; // Set the origin of the RayCast Vector3 rayOrigin = cam.position; // Show the Laser using a Coroutine StartCoroutine(LaserFx()); // Holds the Hit information RaycastHit hit; // Set the origin position of the Laser Line // It will add 10 units down from the ARCamera // We adopted this logic for simplicity Vector3 laserStartPos = new Vector3(cam.position.x, cam.position.y - 2f, cam.position.z); mLaserLine.SetPosition(0, laserStartPos); // Checks if the RayCast hit something if (Physics.Raycast(rayOrigin, cam.forward, out hit, mFireRange)) { // Set the end of the Laser Line to the object hit mLaserLine.SetPosition(1, hit.point); // check target type if (hit.collider.tag == "Enemy") { CubeBehaviorScript cubeCtr = hit.collider.GetComponent <CubeBehaviorScript>(); if (cubeCtr != null) { if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * mHitForce); cubeCtr.Hit(mLaserDamage); } } } } else { // Set the enfo of the laser line to be forward the camera // using the Laser range mLaserLine.SetPosition(1, cam.forward * mFireRange); } }
private void Fire() { Transform cam = Camera.main.transform; mNextFire = Time.time + mFireRate; Vector3 rayOrigin = cam.position; mLaserLine.SetPosition(0, transform.up * -10f); RaycastHit hit; if (Physics.Raycast(rayOrigin, cam.forward, out hit, mFireRange)) { mLaserLine.SetPosition(1, hit.point); CubeBehaviorScript cubeCtr = hit.collider.GetComponent <CubeBehaviorScript>(); CubeBehaviorScript2 cubeCtr2 = hit.collider.GetComponent <CubeBehaviorScript2>(); if (cubeCtr != null) { if (hit.rigidbody != null) { contador = contador + 5; Debug.Log("Tiro"); Debug.Log("Contador " + contador); puntuacion.text = "Marcador: " + contador; hit.rigidbody.AddForce(-hit.normal * mHitForce); cubeCtr.Hit(mLaserDamage); } } if (cubeCtr2 != null) { if (hit.rigidbody != null) { Debug.Log("Incorrecto"); hit.rigidbody.AddForce(-hit.normal * mHitForce); cubeCtr2.Hit(mLaserDamage); } } } else { mLaserLine.SetPosition(1, cam.forward * mFireRange); } StartCoroutine(LaserFx()); }
private void Fire() { Transform cam = Camera.main.transform; mNextFire = Time.time + mFireRate; Vector3 rayOrigin = cam.position; mLaserLine.SetPosition(0, transform.up * -10f); RaycastHit hit; if (Physics.Raycast(rayOrigin, cam.forward, out hit, mFireRange)) { NPalabra = Random.Range(1, LimiteRango); mLaserLine.SetPosition(1, hit.point); CubeBehaviorScript cubeCtr = hit.collider.GetComponent <CubeBehaviorScript>(); CubeBehaviorScript2 cubeCtr2 = hit.collider.GetComponent <CubeBehaviorScript2>(); if (cubeCtr != null) { if (hit.rigidbody != null) { contador = contador + 5; Debug.Log("Tiro"); Debug.Log("Contador " + contador); puntuacion.text = "Marcador: " + contador; hit.rigidbody.AddForce(-hit.normal * mHitForce); cubeCtr.Hit(mLaserDamage); AsignarPalabras(NPalabra); } } if (cubeCtr2 != null) { if (hit.rigidbody != null) { vida = vida - 1; Debug.Log("Incorrecto"); hit.rigidbody.AddForce(-hit.normal * mHitForce); cubeCtr2.Hit(mLaserDamage); AsignarPalabras(NPalabra); } } if (vida <= 0) { Vida1.enabled = false; puntuacionFinal.enabled = true; puntuacionFinal.text = "Puntuacion Total: " + contador; //Application.Quit(); } if (vida == 3) { Vida1.enabled = true; Vida2.enabled = true; Vida3.enabled = true; } if (vida == 2) { Vida1.enabled = true; Vida2.enabled = true; Vida3.enabled = false; } if (vida == 1) { Vida1.enabled = true; Vida2.enabled = false; Vida3.enabled = false; } } else { mLaserLine.SetPosition(1, cam.forward * mFireRange); } StartCoroutine(LaserFx()); }