public virtual int Simulate() { int simulation = -1; Vector3 dir = -transform.forward; Vector3 pos = transform.position + (transform.forward * 0.125f); if (Physics.Raycast(pos, dir, out var hit, 0.175f)) { parentAdjuster = new GameObject().transform; parentAdjuster.position = hit.point; parentAdjuster.SetParent(hit.transform); transform.SetParent(parentAdjuster); float dis = Vector3.Distance(hit.point, pos) + 0.05f + size; if (Physics.SphereCast(pos, size, dir, out var hitDmg, dis, damageLayer)) { DamageZone damaged = hitDmg.transform.GetComponent <DamageZone>(); if (damaged != null) { if (!damaged.DamageableAlreadyDead()) { simulation = Mathf.Max(simulation, 0); if (damaged.Damage(Damage, 1f)) { simulation = 1; } } } } } return(simulation); }
void RaycastShot() { Transform camera = cameraMovement.transform; //Shoot from camera, not gun GunObject gun = gunHandler.gun; Vector3 shotDir = getShotDir(bulletSpread); Vector3 worldDir = camera.TransformDirection(shotDir); Vector3 impactPos = camera.position; impactPos += worldDir * gun.bulletRange; Ray shotRay = new Ray(camera.position, worldDir); if (Physics.Raycast(shotRay, out var hit, gun.bulletRange, collisionLayer)) { DamageZone damaged = null; float dis = Vector3.Distance(camera.position, hit.point) + 0.05f; if (Physics.Raycast(shotRay, out var dmg, dis, damageLayer)) { damaged = dmg.transform.GetComponent <DamageZone>(); } impactPos = hit.point; CreateImpact(impactPos, hit.normal); if (damaged != null) //If we hit something we should damage { if (!damaged.DamageableAlreadyDead()) { bool killed = damaged.Damage(gun.bulletDamage, gun.headshotMult); if (ui) { ui.ShowHitmarker(killed); //Damages and shows hitmarker } } } } Vector3[] pos = { gunHandler.bulletSpawn.transform.position, impactPos }; ShotTrailHelper trail = helper.getAvailableTrail(); trail.Initialize(pos); }