/// <summary> /// Update is called once every frame to update the object. /// </summary> void Update() { if (Input.GetButtonDown("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; StartCoroutine(ShotEffect()); Vector3 rayOrigin = tpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f)); RaycastHit hit; laserLine.SetPosition(0, gunEnd.position); if (Physics.Raycast(rayOrigin, tpsCam.transform.forward, out hit, weaponRange)) { laserLine.SetPosition(1, hit.point); } else { laserLine.SetPosition(1, tpsCam.transform.forward * weaponRange); } ShootableTarget health = hit.collider.GetComponent <ShootableTarget>(); if (health != null) { health.Damage(gunDamage); } if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * hitForce); } } }
void RayCastFunction() { Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f)); //created a bullet from the centre of the screen RaycastHit hit; if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange)) //checks to see whether the ray has hit anything { ShootableTarget health = hit.collider.GetComponent <ShootableTarget>(); //if we hit the object it will get the ShootableTarget script from it, if it exists. if (health != null) //damages the enemy if there is a health script attached { health.Damage(gunDamage); } } }
private void InteractionSourceUpdated(InteractionSourceUpdatedEventArgs controller) { if (Shooting(controller.state)) { hammerPulled = false; animator.SetTrigger("ShootGun"); // Start our ShotEffect coroutine to turn our laser line on and off StartCoroutine(ShotEffect()); // Declare a raycast hit to store information about what our raycast has hit RaycastHit hit; // Set the start position for our visual effect for our laser to the position of gunEnd if (laserLine != null) { laserLine.SetPosition(0, gunEnd.position); } // Check if our raycast has hit anything if (Physics.Raycast(gunEnd.position, gunEnd.transform.forward, out hit, weaponRange)) { // Set the end position for our laser line if (laserLine != null) { laserLine.SetPosition(1, hit.point); } // Get a reference to a health script attached to the collider we hit ShootableTarget health = hit.collider.GetComponent <ShootableTarget>(); // If there was a health script attached if (health != null) { // Call the damage function of that script, passing in our gunDamage variable health.Damage(gunDamage); } // Check if the object we hit has a rigidbody attached if (hit.rigidbody != null) { // Add force to the rigidbody we hit, in the direction from which it was hit hit.rigidbody.AddForce(-hit.normal * hitForce); } } else { // If we did not hit anything, set the end of the line to a position directly in front of the camera at the distance of weaponRange if (laserLine != null) { laserLine.SetPosition(1, gunEnd.position + (gunEnd.transform.forward * weaponRange)); } } } if (PullingHammer(controller.state)) { hammerPulled = true; animator.SetTrigger("LoadGun"); } if (OpeningCylinder(controller.state)) { cylinderOpen = true; animator.SetTrigger("OpenCylinder"); } if (ClosingCylinder(controller.state)) { cylinderOpen = false; animator.SetTrigger("CloseCylinder"); } }
void Update() { // displayBullet(); if (Input.GetButtonDown("Fire1") && Time.time > nextFire && isReload == 0) { if (UFOSpawn.isStarted && !UFOSpawn.isFinished) { numberOfBullet--; DisplayBullet(); } if (numberOfBullet == 0) { isReload = 1; StartCoroutine(reloadBullet()); } 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); // Shootable Target ShootableTarget health = hit.collider.GetComponent <ShootableTarget>(); if (health != null) { health.Damage(gunDamage); } if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * hitForce); } // Shootable Buttons ShootableButton start = hit.collider.GetComponent <ShootableButton>(); if (start != null) { if (start.getButtonType() == 0) { start.hitStart(); } else if (start.getButtonType() == 1) { start.hitRetry(); } else if (start.getButtonType() == 2) { start.hitMenu(); } else if (start.getButtonType() == 3) { start.hitQuit(); } } } else { laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange)); } } }