void OnTriggerEnter(Collider other) { GameObject go = other.gameObject; if (go.name != "Player") { Debug.Log("Sword hit " + go.name); } if (combat.isAttacking) { if (go.tag == "Enemy" || go.tag == "Gun" || go.tag == "Breakable") { Debug.Log("Contact on enemy"); HasHealth health = go.GetComponent <HasHealth> (); if (health != null) { health.ReceiveDamage(damage); } } else if (go.tag == "EnemyBodypart") { go.GetComponent <Rigidbody> ().AddForce(combat.swingDirection * force, ForceMode.Impulse); WeakSpotScript spot = go.GetComponent <WeakSpotScript> (); if (spot != null) { spot.PassDamage(damage); } } } }
public void PassDamage(float damage) { HasHealth health = transform.root.GetComponent <HasHealth> (); health.ReceiveDamage(damage); GameObject bloodParticle = health.bloodPrefab; Instantiate(bloodParticle, this.transform); }
void Explode(Collider hit) { Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius); foreach (Collider c in colliders) { GameObject go; // Attempt to grab gameobject of attached rigidbody. // This is so we can hit compound colliders. try { go = c.attachedRigidbody.gameObject; } catch (NullReferenceException e) { go = c.gameObject; } HasHealth h = go.GetComponent <HasHealth> (); // Skip if collider doesn't have health if (h == null) { continue; } // Deal full damage if collider was hit directly if (c == hit) { h.ReceiveDamage(damage); continue; } // Otherwise, deal damage based on distance. // Damage ratio clamped so that damage doesn't go below 0. float dist = Vector3.Distance(transform.position, go.transform.position); float damageRatio = Mathf.Clamp01(1f - (dist / explosionRadius)); h.ReceiveDamage(damage * damageRatio); } Instantiate(explosionPrefab, transform.position, Quaternion.identity); Destroy(gameObject); }
void OnTriggerEnter(Collider other) { GameObject hitObject = other.gameObject; if (hitObject.tag == "Player" || hitObject.tag == "Enemy") { HasHealth healthSkript = hitObject.GetComponent <HasHealth> (); healthSkript.ReceiveDamage(damage); Debug.Log(transform.gameObject.name + "dealt " + damage + " damage to " + hitObject.name); BlowUp(); } if (hitObject.tag == "EnemyBodypart") { HasHealth healthSkript = hitObject.transform.root.GetComponent <HasHealth> (); healthSkript.ReceiveDamage(damage); Debug.Log(transform.gameObject.name + "dealt " + damage + " damage to " + hitObject.transform.root.gameObject.name); BlowUp(); } else if (hitObject.tag == "Sword" && hasBounced == false) { hasBounced = true; Debug.Log("Bounse of sword"); GameObject player = hitObject.transform.root.gameObject; float randX = Random.Range(-2f, 2f); float randY = Random.Range(-2f, 2f); float randZ = Random.Range(-2f, 2f); direction = new Vector3(direction.x + randX, direction.y + randY, direction.z + randZ); direction = direction * -1f; rig.AddForce(direction * 2f, ForceMode.Impulse); } else if (hitObject.tag == "Trigger") { return; } else { BlowUp(); } }
void melee() { //Ray meleeRay = new Ray(transform.position, direction); var player = GameObject.FindWithTag("Player").transform; //target the player ... again Vector3 direction = (player.transform.position - transform.position).normalized; Ray ray = new Ray(transform.position, direction); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, meleeRange)) { Vector3 hitPoint = hitInfo.point; GameObject go = hitInfo.collider.gameObject; Debug.Log("meleeHit Object: " + go.name); Debug.Log("origin " + transform.position + (transform.forward / 2)); Debug.Log("meleeHit Point: " + hitPoint); Debug.DrawLine(ray.origin, hitInfo.point); HasHealth h = go.GetComponent <HasHealth>(); if (go.tag == "Player") { Debug.Log("DAMAGE " + damage); anim.SetTrigger("melee"); h.ReceiveDamage(damage); if (debrisPrefab != null) { Instantiate(debrisPrefab, hitPoint, Quaternion.identity); } } } }
void OnBulletHit(GameObject go, RaycastHit hit) { HasHealth h = go.GetComponent <HasHealth> (); Rigidbody rb = go.GetComponent <Rigidbody> (); // Deal damage if (h != null) { h.ReceiveDamage(damage); } // Apply push to rigidbody if (rb != null) { Debug.Log("Applying laser push to " + go.name); rb.AddForceAtPosition(fpsCam.transform.forward * damage, hit.point, ForceMode.Impulse); } // Show impact effect if (hitEffectPrefab != null) { Instantiate(hitEffectPrefab, hit.point, Quaternion.LookRotation(hit.normal)); } }
// Update is called once per frame void Update() { coolDownRemaining -= Time.deltaTime; if (Input.GetMouseButton(0) && coolDownRemaining <= 0) { coolDownRemaining = cooldown; Transform cmt = Camera.main.transform; Ray ray = new Ray(cmt.position, cmt.forward); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, range, layerMask)) { Vector3 hitPoint = hitInfo.point; GameObject go = hitInfo.collider.gameObject; // Attempt to grab gameobject of attached rigidbody. // This is so we can hit compound colliders. try { go = hitInfo.collider.attachedRigidbody.gameObject; } catch (NullReferenceException e) {} HasHealth h = go.GetComponent <HasHealth> (); Rigidbody rb = go.GetComponent <Rigidbody> (); // Deal damage if (h != null) { h.ReceiveDamage(damage); } // Apply push to rigidbody if (rb != null) { Debug.Log("Applying laser push to " + go.name); rb.AddForceAtPosition(cmt.forward * damage, hitPoint, ForceMode.Impulse); } // Show impact effect if (hitEffectPrefab != null) { Instantiate(hitEffectPrefab, hitPoint, Quaternion.LookRotation(hitInfo.normal)); } } // Show laser effect. This is after the if statement because we want to see the laser even if it hits nothing. if (lineEffectPrefab != null) { GameObject laser = Instantiate(lineEffectPrefab, shootPoint.position, Quaternion.identity); laser.transform.SetParent(transform); laser.GetComponent <LaserScript> ().lineWidth = lineWidth; if (hitInfo.point == Vector3.zero) { // Laser missed laser.GetComponent <LaserScript> ().SetTarget(cmt.position + (cmt.forward * range)); } else { laser.GetComponent <LaserScript> ().SetTarget(hitInfo.point); } } } }