void Shoot() { //Vector2 spread = new Vector2(0, randSpread); Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y); RaycastHit2D hit = Physics2D.Raycast(firePointPosition, mousePosition - firePointPosition, 100, WhatToHit); Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition) * 100, Color.cyan); if (hit) { Debug.DrawLine(firePointPosition, hit.point, Color.red); Health hp = hit.transform.GetComponent <Health>(); if (hp) { hp.Subtract(damage); Debug.Log("You hit " + hit.collider.name + " and did " + damage + " damage."); } KnockbackBehaviour knockback = hit.transform.GetComponent <KnockbackBehaviour>(); if (knockback) { knockback.Knockback((mousePosition - firePointPosition).normalized * damage * 8); } } shootSound.Play(); if (Time.time >= timeToSpawnEffect) { Vector3 hitPos; Vector3 hitNormal; // An effect that will shoot out perpendicular to what it collides with if (hit.collider == null) { hitPos = (mousePosition - firePointPosition) * 50; // Bullet Effect continues off screen hitNormal = new Vector3(9999, 9999, 9999); } else { hitPos = hit.point; // Bullet Effect stops when it hits something hitNormal = hit.normal; } Effect(hitPos, hitNormal, hit); timeToSpawnEffect = Time.time + 1 / effectSpawnRate; } }
private void OnTriggerEnter2D(Collider2D other) { // Subtract health Health health = other.GetComponent <Health>(); if (health) { health.Subtract(damage); } // Knockback KnockbackBehaviour knockback = other.GetComponent <KnockbackBehaviour>(); if (knockback) { knockback.Knockback((other.transform.position - transform.position).normalized); } }
public virtual void OnHit(Transform source) { KnockBehav.Knockback(transform.position - source.position); }