public static void HandleCollisionEnter(Hittable collidingHittable, GameObject otherCollidingObject) { if (otherCollidingObject.gameObject.TryGetComponent <PlayerLife>(out _)) { return; } Airship collidingAirship = collidingHittable.gameObject.GetComponentInParent <Airship>(); Airship otherCollidingAirship = otherCollidingObject.gameObject.GetComponentInParent <Airship>(); if (collidingAirship && otherCollidingAirship && collidingAirship == otherCollidingAirship) { return; // Airship, don´t hit yourself } Vector3 relativeVelocity = Vector3.zero; int damage = 0; if (collidingAirship && otherCollidingAirship) { relativeVelocity = collidingAirship.Velocity - otherCollidingAirship.Velocity; damage = Mathf.FloorToInt(Vector3.Magnitude(relativeVelocity)); } else { //relativeVelocity = collidingAirship.Velocity; damage = collidingHittable.MaxHealth * 2; } collidingHittable.GetDamage(damage); }
private void OnTriggerEnter(Collider other) { Hittable hittable = other.gameObject.GetComponent <Hittable>(); //try getting Building component if (hittable != null && !hittable.IsEnemy) //if the collider has a building component attached { hittable.GetDamage(damage); //Do damage to the building DestroyBomb(); } }
void ThrowFragments() { RaycastHit hit; Vector3 direction = Random.insideUnitSphere; for (int i = 0; i < fragmentCount; i++) { if (Physics.Raycast(transform.position, direction, out hit, explosionSize)) { Hittable hittable = hit.collider.gameObject.GetComponent <Hittable>(); if (hittable) { hittable.GetDamage(explosionDamage); } } direction = Random.insideUnitSphere; } }
private void FixedUpdate() { if (!destroyed) { Vector3 travelledSegment = transform.position - lastPosition; RaycastHit hit; // TODO: Check for Tag here, too if (Physics.Raycast(lastPosition, travelledSegment, out hit, travelledSegment.magnitude, targetMask)) { // Calculate Damage int impactDamage = Mathf.CeilToInt(rigidbody.Mass * rigidbody.Velocity.magnitude * damage * DamageMod); // Apply Damage Hittable target = hit.collider.GetComponent <Hittable>(); if (target != null) { target.GetDamage(impactDamage); } // Play Hit Sound AudioSource audioSource = hit.collider.GetComponent <AudioSource>(); if (audioSource != null && hitSounds != null && hitSounds.Length > 0) { audioSource.PlayOneShot(hitSounds[Random.Range(0, hitSounds.Length - 1)]); } // Change Bullet Position to Impact Point transform.position = hit.point; // Spawn Fragments at Impact Point travelledSegment = -travelledSegment.normalized; if (impactDamage > 0.0f && fragmentPrefab != null) { int fragmentCount = Mathf.Max(Mathf.FloorToInt(impactDamage * fragmentCountModifier), 1); for (int i = 0; i < fragmentCount; ++i) { // Spawn Fragment and set Speed ((SimpleRigidbody)fragmentPoolManager.getPoolObject(fragmentPrefab, transform.position, transform.rotation, typeof(SimpleRigidbody))).Velocity = (travelledSegment + Random.insideUnitSphere) * fragmentSpeed; } } // Destroy Bullet if (PoolManager != null) { gameObject.SetActive(false); PoolManager.returnPoolObject(this); } else { destroyed = true; GameObject.Destroy(gameObject, 0.02f); } } lastPosition = transform.position; // Make sure, that Tracer Ends never appear inside or behind the Weapon if (tracer != null && (drawTracer || (transform.position - spawnPosition).sqrMagnitude > (rigidbody.Velocity.sqrMagnitude * (tracerLength * tracerLength) * (Time.deltaTime * Time.deltaTime)))) { tracer.SetPosition(0, transform.position); tracer.SetPosition(1, transform.position - (rigidbody.Velocity * tracerLength * Time.deltaTime)); drawTracer = true; } } }