protected virtual void OnCollisionEnter2D(Collision2D col) { GameObject obj = col.gameObject; if (obj.CompareTag("Bullet")) { enemies.Remove(gameObject); GameObject.Find("GameManager").GetComponent <ScoreClass>().Score = point; Destroy(obj); Destroy(gameObject); if (explosion != null) { Instantiate(explosion, transform.position, Quaternion.identity); } AudioSource.PlayClipAtPoint(clip, transform.position); } else if (obj.CompareTag("Player")) { HealthClass hc = obj.GetComponent <HealthClass>(); hc.Health = damage; enemies.Remove(gameObject); Destroy(gameObject); if (explosion != null) { Instantiate(explosion, transform.position, Quaternion.identity); } AudioSource.PlayClipAtPoint(clip, transform.position); } else if (obj.CompareTag("Deathzone")) { enemies.Remove(gameObject); Destroy(gameObject); } }
public void Initialize() { navMeshAgent = GetComponent <NavMeshAgent>(); rb = GetComponent <Rigidbody>(); col = GetComponent <Collider>(); nc_info = GetComponent <NC_Information>(); if (Patrol) { nc_patrol = GetComponent <NC_Patrol>(); nc_patrol.AssignProperties(navMeshAgent, nc_info); nc_patrol.CacheWaypoints(); nc_patrol.SetupWaypoints(navMeshAgent); } nc_sensors = GetComponent <NC_Sensors>(); if (Weaponized) { nc_weapon = GetComponent <NC_Weapon>(); nc_weapon.weapon = nc_weapon.GetComponentInChildren <Weapon>(); } healthClass = GetComponent <HealthClass>(); controller = GetComponent <pNC_StateMachineController>(); }
private void OnTriggerEnter(Collider other) { rb.constraints = RigidbodyConstraints.FreezeAll; currentSpeed = 0; Attack attackStruct; // Damage section. Rigidbody targetRigidbody = other.GetComponent <Rigidbody>(); if (targetRigidbody != null) { HealthClass targetHealth = other.GetComponent <HealthClass>(); if (targetHealth != null) { attackStruct = ValkyDetection.GetAttackStruct(owner, projectileDirection, damage, yKnockbackOffset, knockbackForce, uniformKnockback); targetHealth.TakeDamage(attackStruct); } } Release(); #region old code /* Damage section. * * RaycastHit groundHit; * Physics.Raycast(transform.position, Vector3.down, out groundHit, 20, groundLayer); * Vector3 impostorPos = groundHit.point; * * Rigidbody targetRigidbody = other.GetComponent<Rigidbody>(); * if (targetRigidbody != null) * { * RaycastHit gHit; * Physics.Raycast(other.transform.position, Vector3.down, out gHit, 20, groundLayer); * Vector3 hitImpostorPos = gHit.point; * * Vector3 knockDirection = Vector3.Normalize(impostorPos - hitImpostorPos); * if(knockDirection.Equals(Vector3.zero)) Debug.Log("distance between objects is zero"); * knockback = knockDirection * knockbackForce; * knockback.y = yKnockbackOffset; * Attack attackStruct = new Attack(damage, knockback, uniformKnockback); * * HealthClass targetHealth = other.GetComponent<HealthClass>(); * if (targetHealth == null) * { * return; * } * else * { * targetHealth.TakeDamage(attackStruct); * } * } * Release(); */ #endregion }
// Getting the vector from the projectile direction public static Dictionary <HealthClass, Attack> GetDamageablesObjectsInRange(Transform owner, Vector3 position, float range, bool groundedDetection, Vector3 direction, float damage, float yOffset, float knockbackForce, bool uniformKnockback) { LayerMask groundMask = LayerMask.GetMask("Ground"); LayerMask damagMask = LayerMask.GetMask("Damageables"); Vector3 knockback = Vector3.zero; Dictionary <HealthClass, Attack> hList = new Dictionary <HealthClass, Attack>(); Vector3 gPosition; Vector3 gHitPosition; Attack attackStruct; Collider[] hits = Physics.OverlapSphere(position, range, damagMask); foreach (Collider hit in hits) { Rigidbody tRb = hit.GetComponent <Rigidbody>(); if (tRb != null) { gPosition = position; gHitPosition = hit.transform.position; if (groundedDetection) { RaycastHit gHit; Physics.Raycast(position, Vector3.down, out gHit, 20, groundMask); gPosition = gHit.point; RaycastHit groundHit; Physics.Raycast(hit.transform.position, Vector3.down, out groundHit, 20, groundMask); gHitPosition = groundHit.point; } knockback = direction.normalized * knockbackForce; if (yOffset != 0) { knockback.y = yOffset; } attackStruct = new Attack(damage, knockback, uniformKnockback, owner); HealthClass targetHealth = hit.GetComponent <HealthClass>(); if (targetHealth != null) { hList.Add(targetHealth, attackStruct); } } } return(hList); }
private void OnCollisionEnter(Collision other) { if (hitboxActive) { projectileDirection = Vector3.Normalize(awakePosition - asleepPosition); HealthClass targetHealth = other.gameObject.GetComponent <HealthClass>(); if (targetHealth != null) { rb.Sleep(); Attack attackStruct = ValkyDetection.GetAttackStruct(transform, projectileDirection, GetDamage(), 0, knockbackForce, false); targetHealth.TakeDamage(attackStruct); } } }
public void OnTriggerEnter(Collider other) { if (other.gameObject.layer == groundLayer) { Explosion(false); Release(); } else { rb.Sleep(); currentSpeed = 0; Attack attackStruct; HealthClass healthClass = other.gameObject.GetComponent <HealthClass>(); if (healthClass != null) { attackStruct = ValkyDetection.GetAttackStruct(owner, projectileDirection, damage, yKnockbackOffset, knockbackForce, uniformKnockback); healthClass.TakeDamage(attackStruct); } Release(); } }
private void OnTriggerStay(Collider other) { if (attackType == MeeleType.Persistent) { RaycastHit groundHit; Physics.Raycast(transform.position, Vector3.down, out groundHit, 20, groundMask); Vector3 impostorPos = groundHit.point; Rigidbody targetRigidbody = other.GetComponent <Rigidbody>(); if (targetRigidbody != null) { RaycastHit gHit; Physics.Raycast(other.transform.position, Vector3.down, out gHit, 20, groundMask); Vector3 hitImpostorPos = gHit.point; Vector3 knockDirection = Vector3.Normalize(hitImpostorPos - impostorPos); knockback = knockDirection * knockbackForce; knockback.y = yKnockbackOffset; Attack attackStruct = new Attack(damage, knockback, true, transform); HealthClass targetHealth = other.GetComponent <HealthClass>(); if (targetHealth == null) { targetRigidbody.AddForce(knockback, ForceMode.Impulse); } else { targetHealth.TakeDamage(attackStruct); } } } else { return; } }
void Start() { hitboxActive = false; healthClass = GetComponent <HealthClass>(); rb = GetComponent <Rigidbody>(); }
void Start() { charRigidBody = GetComponent <Rigidbody2D>(); Health = new HealthClass(10); Health.DamageDelegate += TestDamage; }
// Two areas, like the rocket public static Dictionary <HealthClass, Attack> GetDamageablesInRangeTwoSections(Transform owner, Vector3 position, float minRange, float maxRange, Vector3 direction, bool groundDetection, float damage, float yOffset, float knockbackForce, bool uniformKnockback) { Dictionary <HealthClass, Attack> hList = new Dictionary <HealthClass, Attack>(); LayerMask damagMask = LayerMask.GetMask("Damageables"); LayerMask groundMask = LayerMask.GetMask("Ground"); Vector3 knockback = Vector3.zero; Vector3 gPosition; Vector3 gHitPosition; Attack attackStruct; Collider[] hits = Physics.OverlapSphere(position, maxRange, damagMask); foreach (Collider hit in hits) { Rigidbody tRb = hit.GetComponent <Rigidbody>(); if (tRb != null) { //Check distance float hitDistance = Vector3.Distance(hit.transform.position, position); // If object is higher than minimum range, raycast if (hitDistance >= minRange) { gPosition = position; gHitPosition = hit.transform.position; if (groundDetection) { RaycastHit gHit; Physics.Raycast(position, Vector3.down, out gHit, 20, groundMask); gPosition = gHit.point; RaycastHit groundHit; Physics.Raycast(hit.transform.position, Vector3.down, out groundHit, 20, groundMask); gHitPosition = groundHit.point; } Vector3 knockDir = Vector3.Normalize(gHitPosition - gPosition); knockback = knockDir * knockbackForce; if (yOffset != 0) { knockback.y = yOffset; } attackStruct = new Attack(damage, knockback, uniformKnockback, owner); HealthClass targetHealth = hit.GetComponent <HealthClass>(); if (targetHealth != null) { hList.Add(targetHealth, attackStruct); } } else if (hitDistance < minRange) { gPosition = position; gHitPosition = hit.transform.position; if (groundDetection) { RaycastHit gHit; Physics.Raycast(position, Vector3.down, out gHit, 20, groundMask); gPosition = gHit.point; RaycastHit groundHit; Physics.Raycast(hit.transform.position, Vector3.down, out groundHit, 20, groundMask); gHitPosition = groundHit.point; } knockback = direction.normalized * knockbackForce; if (yOffset != 0) { knockback.y = yOffset; } attackStruct = new Attack(damage, knockback, uniformKnockback, owner); HealthClass targetHealth = hit.GetComponent <HealthClass>(); if (targetHealth != null) { hList.Add(targetHealth, attackStruct); } } } } return(hList); }