/// <summary> /// Checks if another ArUcoDrone is in front of the drone and within range. /// Will also check if there's a real object in the way, if checkRealWorldObstaclesBeforeShooting is true. /// </summary> /// <returns>True if there's a valid target in front of the drone.</returns> private bool IsAimingAtTarget() { //First make sure there's a valid virtual target in front of the drone. Ray ray = new Ray(shootAnchorObject.position, shootAnchorObject.rotation * Vector3.forward); RaycastHit[] hits = Physics.RaycastAll(ray, shootRangeMeters); bool foundvirtualtarget = false; float nearestdist = Mathf.Infinity; foreach (RaycastHit hit in hits) { ArUcoDrone otherdrone = hit.transform.GetComponent <ArUcoDrone>(); if (otherdrone != null && otherdrone != this) { foundvirtualtarget = true; if (hit.distance < nearestdist) { nearestdist = hit.distance; } } } if (!foundvirtualtarget) { return(false); } if (checkRealWorldObstaclesBeforeShooting) //Make sure there's not a real-world obstacle in the way of the target. { //If there is one, check to make sure there's not a real-world object in the way. Vector3 collisionpoint; //Not used but required for HitTestOnRay function. foreach (ZEDManager manager in ZEDManager.GetInstances()) { bool hitreal = ZEDSupportFunctions.HitTestOnRay(manager.zedCamera, manager.GetLeftCamera(), shootAnchorObject.transform.position, shootAnchorObject.transform.rotation, nearestdist, 0.01f, out collisionpoint, false, 0.1f); if (hitreal) { return(false); } } return(true); } else { return(true); //We're not checking against the real world, and we already found a virtual object, so fire. } }
/// <summary> /// Called when the projectile hits a virtual object with a collider. /// If the object is an ArUco drone, applies damage. Regardless, spawns an explosion and destroys the projectile. /// </summary> public override void OnHitVirtualWorld(RaycastHit hitinfo) { ArUcoDrone otherdrone = hitinfo.transform.GetComponent <ArUcoDrone>(); if (otherdrone != null && otherdrone != spawningDrone) { otherdrone.TakeDamage(damage); } if (explosionPrefab) { Quaternion explosionrotation = (hitinfo.normal.sqrMagnitude <= 0.001f) ? Quaternion.identity : Quaternion.LookRotation(hitinfo.normal, Vector3.up); GameObject explosion = Instantiate(explosionPrefab, hitinfo.point, explosionrotation); } base.OnHitVirtualWorld(hitinfo); //Destroy. }