public void Shoot() { RaycastHit hit; float distance = 5f; Vector3 direction = transform.forward; Vector3 spread = new Vector3(); spread = spread + (transform.right * Random.Range(-1f, 1f)); direction += spread.normalized * Random.Range(0f, 0.2f); //Vector3 fwd = transform.TransformDirection(Vector3.forward * 10); Ray DetectionRay = new Ray(transform.position, direction); if (Physics.Raycast(DetectionRay, out hit, distance)) { Debug.DrawLine(transform.position, hit.point, Color.green, 1f); if (hit.collider.tag == "Zombie") { //If Zombie is hit, destroy that specific GameObject, and decrease # of zombies Debug.Log("Hit!"); //Checks the zombie health script. ZombieAI health = hit.collider.GetComponent <ZombieAI>(); //this sets how much damage a zombie will take. See why this may not work if its attached to every gun? 1 damage isnt going to cut it if we're using miniguns or shotguns, etc. health.TakeDamage(1, hit.point); if (health != null) { health.TakeDamage(0, hit.point); Debug.Log("Zombie Health: " + health.currentHealth); } //Need health.takedamage(1,hit.point) } //A repeat of above, except with EL DIABLO!! if (hit.collider.tag == "Devil") { DevilScript health = hit.collider.GetComponent <DevilScript>(); //If Zombie is hit, destroy that specific GameObject, and decrease # of zombies Debug.Log("Hit!"); if (health != null) { health.TakeDamage(1, hit.point); Debug.Log("Devil health = " + health.currentHealth); } } } else { Debug.DrawRay(this.transform.position, direction, Color.red, 0.2f); } }
public void gotShot() { fwd = transform.TransformDirection(Vector3.forward * 10); Ray DetectionRay = new Ray(transform.position, fwd); //Physics capsule cast //Gotta figure this out later if (Physics.CapsuleCast(transform.position, fwd, 1.5f, Vector3.forward, out hit, distance)) { if (hit.collider.tag == "Zombie") { Debug.Log("Capsule hit!"); } } if (Physics.Raycast(DetectionRay, out hit, distance)) { //Ray Debugger Debug.DrawRay(transform.position, fwd, Color.green); if (hit.collider.tag == "Zombie") { //If Zombie is hit, destroy that specific GameObject, and decrease # of zombies Debug.Log("Hit!"); //Checks the zombie health script. ZombieAI health = hit.collider.GetComponent <ZombieAI>(); //this sets how much damage a zombie will take. See why this may not work if its attached to every gun? 1 damage isnt going to cut it if we're using miniguns or shotguns, etc. health.TakeDamage(1, hit.point); if (health != null) { health.TakeDamage(0, hit.point); Debug.Log("Zombie Health: " + health.currentHealth); } //Need health.takedamage(1,hit.point) Debug.Log(Zoomzoom.numEnemy); } //A repeat of above, except with EL DIABLO!! if (hit.collider.tag == "Devil") { DevilScript health = hit.collider.GetComponent <DevilScript>(); //If Zombie is hit, destroy that specific GameObject, and decrease # of zombies Debug.Log("Hit!"); if (health != null) { health.TakeDamage(1, hit.point); Debug.Log("Devil health = " + health.currentHealth); } } } }
// Update is called once per frame void Update() { //Checks the forward Facing Direction & raycasts fwd = transform.TransformDirection(Vector3.forward * 10); Ray DetectionRay = new Ray(transform.position, fwd); //Fire by MB1 or Spacebar if (Input.GetMouseButtonDown(0) || Input.GetKeyDown("space")) { //Ray Debugger Debug.DrawRay(transform.position, fwd, Color.cyan); if (Physics.Raycast(DetectionRay, out hit, distance)) { if (hit.collider.tag == "Zombie") { //If Zombie is hit, destroy that specific GameObject, and decrease # of zombies Debug.Log("Hit!"); //Checks the zombie health script. ZombieAI health = hit.collider.GetComponent <ZombieAI>(); //this sets how much damage a zombie will take. See why this may not work if its attached to every gun? 1 damage isnt going to cut it if we're using miniguns or shotguns, etc. health.TakeDamage(damage, hit.point); if (health != null) { health.TakeDamage(0, hit.point); Debug.Log("Zombie Health: " + health.currentHealth); } //Need health.takedamage(1,hit.point) } //A repeat of above, except with EL DIABLO!! if (hit.collider.tag == "Devil") { DevilScript health = hit.collider.GetComponent <DevilScript>(); //If Zombie is hit, destroy that specific GameObject, and decrease # of zombies Debug.Log("Hit!"); if (health != null) { health.TakeDamage(damage, hit.point); Debug.Log("Devil health = " + health.currentHealth); } } } } }
void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "Zombie") { ZombieAI enemy = collision.gameObject.GetComponent <ZombieAI>(); Debug.Log("Contact"); enemy.TakeDamage(3, this.transform.position); Debug.Log(enemy.currentHealth); } }
void Shoot() { myCurrentAmmo--; currentGun.currentAmmo = myCurrentAmmo; RaycastHit thisHit; if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out thisHit, myWepRange)) { //Debug.Log(thisHit.transform.name); ZombieAI myTarget = thisHit.transform.GetComponent <ZombieAI>(); //ZombieAI myTargetHead = thisHit.transform.GetComponentInParent<ZombieAI>(); if (myTarget != null) { // This is for headshots. If we choose, we can implement this later //if (thisHit.transform.tag == "Head Collider") //{ // myTargetHead.HeadshotDie(); // myTargetHead.headshot = true; // Debug.Log("Headshot!"); // if (myTargetHead.impactEffect != null) // { // GameObject zombieHitEffect = Instantiate(myTargetHead.impactEffect, thisHit.point, Quaternion.LookRotation(thisHit.normal)); // Destroy(zombieHitEffect, 0.75f); // } //} //else if (thisHit.transform.tag != "Head Collider") { myTarget.TakeDamage(myWepDamage); myTarget.wasShot = true; myTarget.canMove = false; Debug.Log("Hit!"); // This is incase we want to do a blood effect or something. if (myTarget.impactEffect != null) { GameObject zombieHitEffect = Instantiate(myTarget.impactEffect, thisHit.point, Quaternion.LookRotation(thisHit.normal)); Destroy(zombieHitEffect, 0.75f); } } // end of if else if statement. } // end of the check for a valid target } // end of raycast } // End of shooting animation
void Shoot() { RaycastHit _hit; if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask)) { //debugger, we hit something Debug.Log("hit " + _hit.collider.name); ZombieAI target = _hit.transform.GetComponent <ZombieAI>(); if (target != null) { target.TakeDamage(PlayerWeapon.damage); } } }
// Called every frame after Start(). private void Update() { // If our <targetZombie> is not null, damage him per second. if (targetZombie != null) { if (dpsCounter >= 1f) { targetZombie.TakeDamage(damagePerSecond, shooterID); dpsCounter = 0f; } else { dpsCounter += Time.deltaTime; } } }
public void Shoot() { RaycastHit hit; List <Ray> rays = new List <Ray>(); float distance = 4f; int numberOfRaysToShoot = 4; Vector3 fwd = transform.TransformDirection(Vector3.forward * 10); Ray DetectionRay = new Ray(transform.position, fwd); Ray NewDetectRay; Vector3 direction = transform.forward; Vector3 spread = new Vector3(); for (int i = 0; i < numberOfRaysToShoot; i++) { spread = spread + (transform.right * Random.Range(-1f, 1f)); direction += spread.normalized * Random.Range(0f, 0.2f); //rays.Add(Camera.main.ViewportPointToRay(new Vector3(Random.value, 0f, Random.value))); //fwd = transform.TransformDirection(new Vector3(0,0,0)); rays.Add(NewDetectRay = new Ray(direction, fwd)); } foreach (Ray ray in rays) { if (Physics.Raycast(ray, out hit, distance)) { Debug.DrawLine(transform.position, hit.point, Color.green, 1f); if (hit.collider.tag == "Zombie") { //If Zombie is hit, destroy that specific GameObject, and decrease # of zombies Debug.Log("Hit!"); //Checks the zombie health script. ZombieAI health = hit.collider.GetComponent <ZombieAI>(); //this sets how much damage a zombie will take. See why this may not work if its attached to every gun? 1 damage isnt going to cut it if we're using miniguns or shotguns, etc. health.TakeDamage(1, hit.point); if (health != null) { health.TakeDamage(0, hit.point); Debug.Log("Zombie Health: " + health.currentHealth); } //Need health.takedamage(1,hit.point) } //A repeat of above, except with EL DIABLO!! if (hit.collider.tag == "Devil") { DevilScript health = hit.collider.GetComponent <DevilScript>(); //If Zombie is hit, destroy that specific GameObject, and decrease # of zombies Debug.Log("Hit!"); if (health != null) { health.TakeDamage(1, hit.point); Debug.Log("Devil health = " + health.currentHealth); } } } else { Debug.DrawRay(this.transform.position, direction, Color.red, 0.2f); } } }
// Will apply damage to any zombies within the Overlap Sphere. private void OverlapSphereDamage() { Collider[] hitColliders = Physics.OverlapSphere(transform.position, blastRadius); foreach (Collider collider in hitColliders) { Transform hitTransform = collider.transform; if (hitTransform.root.tag == "Zombie") { // If we have already iterated through a Zombie containing this collider, then skip to next. if (hitZombies.Contains(hitTransform.root)) { continue; } // Add the new zombie to the <hitZombies> so that he don't get iterated more than once. hitZombies.Add(collider.transform.root); ZombieAI targetZombie = collider.transform.root.GetComponent <ZombieAI>(); if (throwableType == ThrowableType.Molotov) { foreach (Transform child in targetZombie.transform) { // If the zombie is already on fire, there is not need to set him on fire. if (child.GetComponent <FireDamage>() != null) { continue; } } // Create a fire particle (does apply damage by itself). GameObject fireClone = Instantiate(fireParticle, collider.transform.position, Quaternion.identity) as GameObject; fireClone.name = "Flame"; fireClone.transform.SetParent(targetZombie.transform, true); fireClone.transform.localPosition = Vector3.zero; // Setup the fire particle FireDamage component. FireDamage fireDamage = fireClone.GetComponent <FireDamage>(); fireDamage.shooterID = shooterID; fireDamage.targetZombie = targetZombie; } else { // Set the grenade explosion to this one (for ExplosionForce physics) and apply damage to the zombie. targetZombie.grenadeExplosion = this; targetZombie.TakeDamage(damage, shooterID); } } else if (throwableType == ThrowableType.Grenade) { Rigidbody targetRigidbody = hitTransform.GetComponent <Rigidbody>(); if (targetRigidbody != null) { targetRigidbody.AddExplosionForce(damage * 10f, transform.position, blastRadius, 2f); } } } if (throwableType == ThrowableType.Grenade) { Destroy(gameObject); } }
// Update is called once per frame void Update() { if (health <= 0) { Time.timeScale = 0; chMotor.canControl = false; return; } //float vScale = 1.0f; float speed = walkSpeed; if (Input.GetButton("Fire1")) { string anim = (zoomed) ? "shoot_zoom" : "shoot"; if (!anims.IsPlaying(anim) && anims.Play(anim)) { RaycastHit hit = RayCast.Raycast(GameObject.Find("flashlight").transform, 100); GameObject objHit = hit.transform.gameObject; ZombieAI zombie = objHit.GetComponentInParent <ZombieAI>(); if (!zombie.isDead()) { if (objHit.tag == "ZombieHead") { zombie.TakeDamage(60); } if (objHit.tag == "ZombieCollider") { zombie.TakeDamage(40); } } } } if (Input.GetButton("Sprint") && !crouching) { speed = runSpeed; } if (Input.GetButtonDown("Crouch")) { crouching = !crouching; } if (Input.GetButton("Reload") || (Input.GetButton("Crouch") && Input.GetButton("Reload"))) { if (zoomed) { anims.Play("unzoom"); anims.PlayQueued("reload"); zoomed = false; } anims.Play("reload"); } if (anims.IsPlaying("reload")) { reloading = true; } else { reloading = false; } if (Input.GetMouseButton(1)) { if (zoomed || reloading) { return; } anims.Play("zoom"); zoomed = true; } else { if (!zoomed) { return; } anims.Play("unzoom"); zoomed = false; } chMotor.movement.maxForwardSpeed = speed; if (!anims.IsPlaying("shoot") && !anims.IsPlaying("shoot_zoom") && !anims.IsPlaying("reload") && !anims.IsPlaying("unzoom") && !anims.IsPlaying("zoom")) { anims.Play("idle"); } }
// Will calculate the trajectory of a weapon projectile, affecting the total trajectory distance // depending on the penetration value of the hit DecalMaterial's on the trajectory. // Will also apply damage to Zombies if they are within the trajectory. public void CalculatePenetration(Weapon shotWeapon, NetworkInstanceId shooterID) { float shotDistance = shotWeapon.fireDistance; Transform shootPoint = shotWeapon.shootPoint; RaycastHit hit = shotWeapon.hit; Ray entryRay = new Ray(shootPoint.position, shootPoint.forward); // Calculate the entry raycast and get all the hit information. RaycastHit[] entryHits = Physics.RaycastAll(entryRay, shotDistance, ~(1 << 8), QueryTriggerInteraction.Ignore); // Sort the entry hits from smallest to biggest distance. Array.Sort(entryHits, delegate(RaycastHit hitA, RaycastHit hitB) { return(hitA.distance.CompareTo(hitB.distance)); }); // Get the end point of the total trajectory, so we can raycast back to the origin. Vector3 rayEndPoint = shootPoint.position + (shootPoint.forward * shotDistance); float offsetDistance = Vector3.Distance(rayEndPoint, hit.point); Ray exitRay = new Ray(rayEndPoint, hit.point - rayEndPoint); // Calculate the exit raycast and get all the hit information. RaycastHit[] exitHits = Physics.RaycastAll(exitRay, offsetDistance, ~(1 << 8), QueryTriggerInteraction.Ignore); // Sort the exit hits from smallest to biggest distance. Array.Sort(exitHits, delegate(RaycastHit hitA, RaycastHit hitB) { return(hitA.distance.CompareTo(hitB.distance)); }); float finalTravelDistance = shotDistance; for (int i = 0; i < entryHits.Length; i++) { // Calculate the entry hits and decals. // If the root object of the collider we hit in the trajectory is a zombie, then apply damage to him. if (entryHits[i].transform.root.tag == "Zombie") { ZombieAI hitZombie = entryHits[i].transform.root.GetComponent <ZombieAI>(); // If we hit the Zombie's head, subtract all he has left for health from his remainder health. float totalDamage = (entryHits[i].collider.name == "Head") ? hitZombie.currentHealth : shotWeapon.shotDamage; // Apply the damage to the zombie. hitZombie.TakeDamage(totalDamage, shooterID); } // Get the DecalMaterial from this entry hit object. DecalMaterial hitMaterial = entryHits[i].collider.GetComponent <DecalMaterial>(); // If this entry hit object does not have a DecalMaterial, then return and ignore further calculations for this entry hit. if (hitMaterial == null) { continue; } // Check if the entry point of the projectile was still inside the current final travel distance // after penetration discounts have been made. if (entryHits[i].distance <= finalTravelDistance) { CreateDecal(entryHits[i], hitMaterial, DecalType.Entry); // Check if there is a object hit after this entry hit (for splatter calculations). if (entryHits.Length - 1 >= i + 1) { // Get the entry hit after this one. RaycastHit splatterHit = entryHits[i + 1]; // Check if the current hit material splatters and that the next hit object is within splatter range. if (hitMaterial.doesSplatter && (splatterHit.distance <= splatterRange)) { CreateDecal(splatterHit, hitMaterial, DecalType.Splatter); } } } else { break; } // Affect the final travel distance depending on the hit objects material penetrability. finalTravelDistance *= hitMaterial.penetrability; // Check if there is a exit hit for the current entry hit. if (exitHits.Length - 1 >= i) { RaycastHit currentExitHit = exitHits[exitHits.Length - 1 - i]; // Check if the exit decal is within the projectile travel distance. if (Vector3.Distance(currentExitHit.point, shootPoint.position) <= finalTravelDistance) { CreateDecal(currentExitHit, hitMaterial, DecalType.Exit); } } } Debug.DrawRay(shootPoint.position, shootPoint.forward * finalTravelDistance, Color.blue, 2000f); }