// Update is called once per frame void Update() { cooldownRemaining -= Time.deltaTime; if (Input.GetMouseButton(0) && cooldownRemaining <= 0) { cooldownRemaining = cooldown; Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, range)) { Vector3 hitPoint = hitInfo.point; GameObject go = hitInfo.collider.gameObject; //Debug.Log("Hit Object: "+go.name); //Debug.Log("Hit Point: "+ hitPoint); HasHealth h = go.GetComponent <HasHealth>(); if (h != null) { h.RecieveDamage(50f); } if (debrisPrefab != null) { Instantiate(debrisPrefab, hitPoint, Quaternion.identity); //effectSpawn = new Vector3 (col.transform.position.x, col.transform.position.y, col.transform.position.z); Instantiate(sparkEffect, hitPoint, Quaternion.identity); } } } }
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 Explode(Dieable dieable) { Collider2D[] cols = Physics2D.OverlapCircleAll(transform.position, damageRadius); foreach (var col in cols) { Rigidbody2D rb = col.GetComponent <Rigidbody2D>(); if (rb == null) { continue; } Dieable lel = rb.GetComponent <Dieable>(); if (lel == dieable) { continue; } Vector3 displacement = rb.transform.position - transform.position; rb.AddForce(displacement.normalized * knockback, ForceMode2D.Impulse); HasHealth health = rb.GetComponent <HasHealth>(); if (health != null) { if (col.tag == "Player") { health.Damage(damagePlayer); } else if (col.tag == "Enemy" && !col.gameObject.GetComponent <Enemy>().isDead) { health.Damage(damageEnemy); } } } sRenderer.color = Color.white; timer.StartTimer(fadeoutTime); exploded = true; }
private void OnTriggerEnter2D(Collider2D other) { Debug.Log("Object that entered the trigger: " + other); HasHealth health = other.GetComponent <HasHealth>(); if (health != null) { health.ChangeHealth(healAmount); } }
public void PassDamage(float damage) { HasHealth health = transform.root.GetComponent <HasHealth> (); health.ReceiveDamage(damage); GameObject bloodParticle = health.bloodPrefab; Instantiate(bloodParticle, this.transform); }
private void OnCollisionStay2D(Collision2D collision) { if (collision.gameObject.layer == LayerMask.NameToLayer("Entities")) { HasHealth hasHealth = collision.gameObject.GetComponent <HasHealth>(); if (hasHealth != null) { hasHealth.Damage(Damage); } } }
void OnTriggerEnter2D(Collider2D collision) { Debug.Log(collision.gameObject.name + " on layer " + collision.gameObject.layer + " colliding with " + gameObject.name + " on layer " + gameObject.layer); HasHealth other = collision.gameObject.GetComponent <HasHealth>(); if (other != null) { other.TakeDamage(damage); if (selfDestruct) { Destroy(gameObject); } } }
// Use this for initialization private void Start() { c_transform = GetComponent <Transform>(); c_gameObject = GetComponent <GameObject>(); health = GetComponent <HasHealth>(); unitAnimator = GetComponent <AnimatorManager>(); if (health == null) { Debugger.printErrorLog("'" + name + "(" + entityType + ")' doesn't have a <HasHealth> COMPONENT."); } if (unitAnimator == null) { Debugger.printErrorLog("'" + name + "' doesn't have a <AnimatorManager(Script)> COMPONENT."); } }
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(); } }
private void OnCollisionEnter2D(Collision2D collision) { GameObject obj = collision.gameObject; if (obj.tag == "Bullet" && obj.tag != "Wall") { return; } if (obj.tag == "Wall" || (Time.time - lastHit > safeTime && IsFatal())) { HasHealth health = obj.GetComponent <HasHealth>(); if (health != null && Damages(obj.tag)) { health.Damage(damage); } Destroy(gameObject); } lastHit = Time.deltaTime; }
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)); } }
void Start() { HasHealthScript = GetComponent <HasHealth>(); HasShieldScript = GetComponent <HasShield>(); }
private void Awake() { _player = GameObject.FindGameObjectWithTag(Tags.PLAYER).transform; _health = gameObject.GetComponent <HasHealth>(); _animator = GetComponentInChildren <Animator>(); }
//################################################################################################# //### UnityEngine void Start() { player = GameObject.FindGameObjectWithTag("Player"); playerHealth = player.GetComponent<HasHealth>(); health = gameObject.GetComponent<HasHealth>(); range = Global.global.levelSize / 2.0f; if(!spawner) { spawner = GameObject.FindGameObjectWithTag("Spawner").GetComponent<SpawnEnemies>(); } }
// 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); } } } }
void Start() { HasHealthScript = GetComponent <HasHealth>(); }
// Use this for initialization private void Start() { hp = gameObject.GetComponent<HasHealth>(); base.Start(); }