636{
637 TRACE_CPUPROFILER_EVENT_SCOPE(LineOfSight_Trace);
638 SCOPE_CYCLE_COUNTER(STAT_LineOfSightLineTrace);
639
641
643 {
645 }
646
647 TSet<TWeakObjectPtr<AActor>> AllHitActorTSet;
648 const FVector ComponentLocation = GetComponentLocation();
649
650 float Step1;
651 FVector direction1Neg;
652 FVector direction1Pos;
653 float posNegative1XOrig = 0;
654 float posNegative1YOrig = 0;
655
657 {
658 Step1 = 0.0f;
659 }
660 else
661 {
662 float posNegative1X = FMath::Cos(FMath::DegreesToRadians(
Angle1 * -1)) *
Radius1;
663 float posNegative1Y = FMath::Sin(FMath::DegreesToRadians(
Angle1 * -1)) *
Radius1;
664
665 float posPositive1X = FMath::Cos(FMath::DegreesToRadians(
Angle1)) *
Radius1;
666 float posPositive1Y = FMath::Sin(FMath::DegreesToRadians(
Angle1)) *
Radius1;
667
668 posNegative1XOrig = posNegative1X;
669 posNegative1YOrig = posNegative1Y;
670
671 posNegative1X += ComponentLocation.X;
672 posNegative1Y += ComponentLocation.Y;
673 posPositive1X += ComponentLocation.X;
674 posPositive1Y += ComponentLocation.Y;
675
676 FVector Point1Neg(posNegative1X, posNegative1Y, ComponentLocation.Z);
677 FVector Point1Pos(posPositive1X, posPositive1Y, ComponentLocation.Z);
678
679 float Distance1 = FMath::Abs(posPositive1Y - posNegative1Y);
681
682 direction1Neg = (Point1Neg - ComponentLocation).GetSafeNormal();
683 direction1Pos = (Point1Pos - ComponentLocation).GetSafeNormal();
684 }
685
686 float posNegative2X = FMath::Cos(FMath::DegreesToRadians(
Angle2 * -1)) *
Radius2;
687 float posNegative2Y = FMath::Sin(FMath::DegreesToRadians(
Angle2 * -1)) *
Radius2;
688
689 float posPositive2X = FMath::Cos(FMath::DegreesToRadians(
Angle2)) *
Radius2;
690 float posPositive2Y = FMath::Sin(FMath::DegreesToRadians(
Angle2)) *
Radius2;
691
692 const float posNegative2XOrig = posNegative2X;
693 const float posNegative2YOrig = posNegative2Y;
694
695 posNegative2X += ComponentLocation.X;
696 posNegative2Y += ComponentLocation.Y;
697 posPositive2X += ComponentLocation.X;
698 posPositive2Y += ComponentLocation.Y;
699
700 const FVector Point2Neg(posNegative2X, posNegative2Y, ComponentLocation.Z);
701 const FVector Point2Pos(posPositive2X, posPositive2Y, ComponentLocation.Z);
702
703 const float Distance2 = FMath::Abs(posPositive2Y - posNegative2Y);
705
706 const FVector direction2Neg = (Point2Neg - ComponentLocation).GetSafeNormal();
707 const FVector direction2Pos = (Point2Pos - ComponentLocation).GetSafeNormal();
708
709 FVector directionRotate1Neg;
710 FVector directionRotate1Pos;
711 const FRotator RotationComponent = GetComponentRotation();
712
713
715 {
716 directionRotate1Neg = ComponentLocation;
717 directionRotate1Pos = ComponentLocation;
718 }
719 else
720 {
721 directionRotate1Neg = direction1Neg.RotateAngleAxis(RotationComponent.Roll * -1, FVector(1, 0, 0));
722 directionRotate1Neg = directionRotate1Neg.RotateAngleAxis(RotationComponent.Pitch * -1, FVector(0, 1, 0));
723 directionRotate1Neg = directionRotate1Neg.RotateAngleAxis(RotationComponent.Yaw, FVector(0, 0, 1));
724 directionRotate1Neg = (directionRotate1Neg *
Radius1) + ComponentLocation;
725
726 directionRotate1Pos = direction1Pos.RotateAngleAxis(RotationComponent.Roll * -1, FVector(1, 0, 0));
727 directionRotate1Pos = directionRotate1Pos.RotateAngleAxis(RotationComponent.Pitch * -1, FVector(0, 1, 0));
728 directionRotate1Pos = directionRotate1Pos.RotateAngleAxis(RotationComponent.Yaw, FVector(0, 0, 1));
729 directionRotate1Pos = (directionRotate1Pos *
Radius1) + ComponentLocation;
730 }
731
732
733 FVector directionRotate2Neg = direction2Neg.RotateAngleAxis(RotationComponent.Roll * -1, FVector(1, 0, 0));
734 directionRotate2Neg = directionRotate2Neg.RotateAngleAxis(RotationComponent.Pitch * -1, FVector(0, 1, 0));
735 directionRotate2Neg = directionRotate2Neg.RotateAngleAxis(RotationComponent.Yaw, FVector(0, 0, 1));
736 directionRotate2Neg = (directionRotate2Neg *
Radius2) + ComponentLocation;
737
738 FVector directionRotate2Pos = direction2Pos.RotateAngleAxis(RotationComponent.Roll * -1, FVector(1, 0, 0));
739 directionRotate2Pos = directionRotate2Pos.RotateAngleAxis(RotationComponent.Pitch * -1, FVector(0, 1, 0));
740 directionRotate2Pos = directionRotate2Pos.RotateAngleAxis(RotationComponent.Yaw, FVector(0, 0, 1));
741 directionRotate2Pos = (directionRotate2Pos *
Radius2) + ComponentLocation;
742
743 const FVector Direction1AfterRotation = (directionRotate1Pos - directionRotate1Neg).GetSafeNormal();
744 const FVector Direction2AfterRotation = (directionRotate2Pos - directionRotate2Neg).GetSafeNormal();
745
747 {
748 FVector direction1Step;
750 {
751 direction1Step = GetComponentLocation();
752 }
753 else
754 {
755 direction1Step = (Direction1AfterRotation * (Step1 * i)) + directionRotate1Neg;
756 }
757
758 FVector direction2Step = (Direction2AfterRotation * (Step2 * i)) + directionRotate2Neg;
759
760 FVector ClearPosition1(posNegative1XOrig, posNegative1YOrig + (Step1 * i), 0);
761 FVector ClearPosition2(posNegative2XOrig, posNegative2YOrig + (Step2 * i), 0);
763 {
764 ClearPosition1 = FVector::ZeroVector;
765 }
766
767 TArray< FHitResult> HitResult;
768 const bool hit = GetWorld()->LineTraceMultiByChannel(HitResult, direction1Step, direction2Step,
TraceChanne2,
TraceParams);
769
771 {
772 for (auto& val : HitResult)
773 {
774 if (AllHitActorTSet.Contains(val.GetActor()) == false)
775 {
776 AllHitActorTSet.Add(val.GetActor());
777
778 if (HitEveryFrameEvent)
779 {
781 }
782
784 return HitResult.GetActor() == val.GetActor();
785 }))
786 {
789 }
790 }
791 }
792 }
793
794 if (hit)
795 {
796 const float distance = UKismetMathLibrary::Vector_Distance(direction1Step, HitResult.Last().Location);
797 const FVector DirectionHit = UKismetMathLibrary::GetDirectionUnitVector(ClearPosition1, ClearPosition2);
798 const FVector PositionHit = (DirectionHit * distance) + ClearPosition1;
799
801 {
802
804 }
805
806
808 }
809 else
810 {
812 {
814 }
815
817 }
818
819#if WITH_EDITORONLY_DATA
821 {
822 FVector HitLocation = (hit) ? HitResult.Last().Location : direction2Step;
823 DrawDebugLine(GetWorld(), direction1Step, HitLocation, FColor::Orange, false, -1.f, 0, 1);
824 }
825#endif
826 }
827
829 {
831 {
833 {
835 {
837 }
839 }
840 }
841 }
842}