Line trace ricochet problem

Hi!
I’m trying to implement a ricochet mechanic in c++ and i’m following this guide A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

My project:
I started a default “First person” template and changed the code from OnFire located in the default character with the following lines

void ATest_RicochetCharacter::OnFire()
{
	FHitResult Hit, HitBounce;
	//start a bit forward from camera
	FVector Start = FirstPersonCameraComponent->GetComponentLocation() + FirstPersonCameraComponent->GetForwardVector() * 150;
	FVector End = FirstPersonCameraComponent->GetComponentLocation() + FirstPersonCameraComponent->GetForwardVector() * 2150;
	GetWorld()->LineTraceSingleByChannel(Hit, Start, End,ECollisionChannel::ECC_WorldStatic ,FCollisionQueryParams::DefaultQueryParam);
	//if it hits = ricochet and draw debug line from start to hit location
	if (Hit.bBlockingHit) {
		DrawDebugLine(GetWorld(), Hit.TraceStart, Hit.Location, FColor::, false, 5.0f);
		GetWorld()->LineTraceSingleByChannel(HitBounce, Hit.ImpactPoint + Hit.Normal, (End).MirrorByVector(Hit.ImpactNormal), ECollisionChannel::ECC_WorldStatic,FCollisionQueryParams::DefaultQueryParam);
		//if it hits = draw debug line from hitbounce start to hitbounce location
		if (HitBounce.bBlockingHit) {
			DrawDebugLine(GetWorld(), HitBounce.TraceStart, HitBounce.Location, FColor::Green, false, 5.0f);
		}
		else {
			DrawDebugLine(GetWorld(), HitBounce.TraceStart, HitBounce.TraceEnd, FColor::Black, false, 5.0f);
		}
	}
	else {
		DrawDebugLine(GetWorld(), Hit.TraceStart, Hit.TraceEnd, FColor::Black, false, 5.0f);
	}
}

This are the results i got:

Seems to work

but I aim up a little bit

second image with zoom

seems to work on the side

but i aim a little to the right and this happens

it looks like more angle = worst result

Any comments would be appreciated

Hello Tk-14-,

I’ve been looking into this issue a bit over the past couple days and it definitely seems odd. The math you’re using looks right, even matches up with one of our blueprint tutorials. I’ll continue looking at this, just wanted you let you know that the post isn’t being ignored.

thanks a lot! hope you find the solution to this issue :slight_smile:

I have to admit, this has me pretty stumped. I’m still looking into what could be causing this problem and I’ll let you know as soon as I do, just wanted to give a little update.

thanks for keeping me updated! it’s a must have feature for my game so i’m cheking every day xD

In line 11 you have:

GetWorld()->LineTraceSingleByChannel(HitBounce, Hit.ImpactPoint + Hit.Normal, (End).MirrorByVector(Hit.ImpactNormal), ECollisionChannel::ECC_WorldStatic,FCollisionQueryParams::DefaultQueryParam);

I think the third parameter should be:

(End-Start).MirrorByVector(Hit.ImpactNormal) + Hit.ImpactPoint

Good call, rantrod. That seems to work perfectly. I figured it was something with that math but couldn’t quite nail it down.

Thanks! it’s working now. But this should be fixed in the example given by unreal A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums

I’ll be sure that it gets fixed. It could use an update either way as it was made for 4.6.1.

The tutorial is now updated! thanks everyone :slight_smile: