How to rotate socket attached to skeletal mesh in order to get linetrace to fire in different direction

I am using a rand range function in order to randomize the socket’s rotation. For some reason I try to add to the pitch yaw or roll of the socket and the line trace stays pointing straight forward instead of veering based on the new angle that I am implementing.

FCollisionResponseParams ResponseParams;
		FCollisionQueryParams QueryParams = FCollisionQueryParams::DefaultQueryParam;
		FActorSpawnParameters SpawnParams;
		SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
		QueryParams.AddIgnoredComponent(BaseMesh);
		TArray<FHitResult> Hit;
		const FVector StartTrace = BaseMesh->GetSocketLocation(FName("Socket"));
		FRotator CurrentRotation = BaseMesh->GetSocketRotation(FName("Socket"));
		 FVector EndTrace = StartTrace + CurrentRotation.Vector() * 10000.0f;
		if (GetWorld()->LineTraceMultiByChannel(Hit, StartTrace, EndTrace, ECollisionChannel::ECC_Visibility, QueryParams, ResponseParams))
		{
           for(FHitResult &Results :  Hit)
           {
				 int InMin = 1;
				 int InMax = 10;
				 float PitchAndRoll = CurrentRotation.Pitch + CurrentRotation.Roll;
                                 //Randomizing the aim.
				 int RandomAim = FMath::RandRange(InMin, InMax);

				 if (RandomAim <= 2)
				 {
					 GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::Orange, FString::Printf(TEXT("CurrentRotation: %d"), RandomAim));
                                       //I am trying to rotate the socket here.
					 CurrentRotation.Pitch += 20.0f;
					
				 }
				 else if (RandomAim >= 2)
				 {
					 GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::Orange, FString::Printf(TEXT("CurrentRotation: %d"), RandomAim));
                                         //here too
					 CurrentRotation.Pitch += 20.0f;
				 }
				 else if (RandomAim <= 4)
				 {
					 GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::Orange, FString::Printf(TEXT("CurrentRotation: %d"), RandomAim));
					 CurrentRotation.Pitch += 20.0f;
				 }
				 else if (RandomAim <= 10)
				 {
					 GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::Orange, FString::Printf(TEXT("CurrentRotation: %d"), RandomAim));
					 CurrentRotation.Pitch += 20.0f;
				 }
			 }
	DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor::Red, false, 1.0f);
		 }
       }
}

The EndTrace value is only being set before you rotate the socket. Try updating it after making the rotation

1 Like

Thank you I solved it before your answer. I figured it out what you said is exactly what is was.

1 Like