public override float GetRotation() { scannerRay.Clear(); //on execute le scan des 3 zones autour de nous avec les raycasts float angleStep = angleScannedRadian / (raycastNumber - 1); int nbrRayCasted = 0; for (float angle = -angleScannedRadian / 2; nbrRayCasted < raycastNumber; angle += angleStep) { Vector2 translatedVector = Trigonometry.RotateVector(transform.up, angle); RaycastHit2D[] result = new RaycastHit2D[1]; Physics2D.Raycast( origin: raycastStartPosition.transform.position, direction: translatedVector, contactFilter: new ContactFilter2D().NoFilter(), results: result, distance: distanceCaptureRay ); nbrRayCasted++; if (result[0].collider.CompareTag("Player")) { Debug.DrawRay(raycastStartPosition.transform.position, translatedVector, Color.green); } scannerRay.AddRay(result[0], angle); } //on regarde quelle zone présente un danger le plus faible ZoneScanRay zoneDecision = scannerRay.SafestZone(); //si la zone choisie est tout de même très dangereuse if (zoneDecision.danger >= 500) { //alors on utilise le pouvoir de saut useSecondaryBonus = true; } if (scannerRay.isEnnemyInFrontZone()) { usePrimaryBonus = true; } else { usePrimaryBonus = false; } return(zoneDecision.decision); }
public override float GetRotation() { List <RaycastHit2D> raycastList = new List <RaycastHit2D>(); float angleStep = Mathf.PI / (raycastNumber - 1); for (float angle = Mathf.PI / 2; raycastList.Count < raycastNumber; angle -= angleStep) { Vector2 translatedVector = Trigonometry.RotateVector(transform.up, angle); RaycastHit2D[] result = new RaycastHit2D[1]; Physics2D.Raycast( origin: raycastStartPosition.transform.position, direction: translatedVector, contactFilter: new ContactFilter2D().NoFilter(), results: result, distance: 20 ); raycastList.Add(result[0]); } RaycastHit2D closest = raycastList.First(); foreach (RaycastHit2D raycastArr in raycastList) { if (raycastArr.distance < closest.distance) { closest = raycastArr; } } float detectionDistance = 0.4f; if (closest.distance > detectionDistance) { return(KeepForward()); } else { Vector2 recommendedTrajectory = (Vector2)transform.up + closest.normal; float angleTrajectory = Vector2.SignedAngle(transform.up, recommendedTrajectory); if (angleTrajectory < 0) { return(TurnRight()); } else { return(TurnLeft()); } } }
private string SaveRaycasts() { string hitResults = ""; float raycastAngle = nbRaycasts > 1 ? -0.5f * angleShipFieldview : 0; Vector2 direction; for (int i = 0; i < nbRaycasts; i++) { direction = Trigonometry.RotateVector(transformShipPlayer.up, raycastAngle * Mathf.Deg2Rad); hitResults += RunRaycast(direction) + ";"; raycastAngle += angleDiffBetweenRaycast; } return(hitResults); }