private void Start() { Vector3 towardsPlayer = Camera.main.transform.position - transform.position; transform.rotation = Quaternion.LookRotation(towardsPlayer, Vector3.up); spawnTime = Time.time; PistolTarget pistolTarget = GetComponent <PistolTarget>(); if (pistolTarget != null) { pistolTarget.OnHit += (hitGameObject => { Disappear(); ParticleSystem.MainModule particleSystemMain = GetComponent <ParticleSystem>().main; particleSystemMain.simulationSpeed *= 3; }); } }
public void OnTriggerPressed(object sender, ClickedEventArgs e) { PistolCharge pistolCharge = GetComponent <PistolCharge>(); if (pistolCharge != null && pistolCharge.isActiveAndEnabled) { if (pistolCharge.currentShots < 1) { return; } pistolCharge.currentShots -= 1.0f; } PistolTarget closestPistolTarget = null; float closestDistance = float.PositiveInfinity; Vector3 pointOnTarget = Vector3.zero; foreach (PistolTarget pistolTarget in PistolTarget.instances) { SphereCollider targetSphere = pistolTarget.sphereCollider; Vector3 targetPosition = targetSphere.transform.TransformPoint(targetSphere.center); Vector3 localTargetPosition = muzzleTransform.InverseTransformPoint(targetPosition); float distanceMuzzleToTargetSphere = localTargetPosition.magnitude; if (distanceMuzzleToTargetSphere > 0) // else division by zero { var angleToTarget = Vector3.Angle(localTargetPosition, Vector3.forward); float additionalSlackDegrees = Mathf.Atan(targetSphere.radius / distanceMuzzleToTargetSphere) * Mathf.Rad2Deg; if (angleToTarget <= aimBotMaxAngleDegrees + additionalSlackDegrees) { // ok, target is in viewcone of weapon's aimbot Vector3 pointNearTarget = muzzleTransform.position + muzzleTransform.forward * Mathf.Max(distanceMuzzleToTargetSphere - targetSphere.radius, 0); pointOnTarget = targetSphere.ClosestPoint(pointNearTarget); // this point would actually be shot at Debug.DrawLine(muzzleTransform.position, pointOnTarget, Color.green); RaycastHit raycastHit; if (Physics.Raycast(muzzleTransform.position, (pointOnTarget - muzzleTransform.position).normalized, out raycastHit, float.MaxValue, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Collide)) { if (raycastHit.collider == targetSphere) { // ok, line of sight is free closestDistance = localTargetPosition.magnitude; closestPistolTarget = pistolTarget; } } } } } Vector3 shotDirection; if (closestPistolTarget != null) { Debug.DrawLine(muzzleTransform.position, pointOnTarget, Color.blue, 1.0f); shotDirection = (pointOnTarget - muzzleTransform.position).normalized; closestPistolTarget.Hit(); } else { shotDirection = muzzleTransform.forward; } if (muzzleFlashPrefab != null) { Instantiate(muzzleFlashPrefab, muzzleTransform.position, muzzleTransform.rotation); } { RaycastHit raycastHit; if (Physics.Raycast(muzzleTransform.position, shotDirection, out raycastHit, float.MaxValue, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Collide)) { if (bulletImpactPrefab != null && closestPistolTarget == null) { Instantiate(bulletImpactPrefab, raycastHit.point, Quaternion.FromToRotation(Vector3.forward, raycastHit.normal), raycastHit.transform); } if (volumetricLineBehaviorPrefab != null) { var volumetricLineBehavior = Instantiate(volumetricLineBehaviorPrefab); volumetricLineBehavior.StartPos = muzzleTransform.position / volumetricLineBehaviorPrefab.transform.localScale.x; volumetricLineBehavior.EndPos = raycastHit.point / volumetricLineBehaviorPrefab.transform.localScale.x; } } } }