How to adjust the direction of the missile according to the crosshair - problem

I’m working on simple game Anti Tank Guided Missile, which the missile changes the direction according to the crosshair on the middle of screen controlled by user’s mouse.

Firstly, I guided the crosshair beside the far cube then i fired the missile with re-guided the crosshair on to the cube.

The problem is, the direction of missile was changed based on the crosshair but with large error rate ( Green Debug Line of the Missile in the video shows that problem )

PlayerController - AimAt Function

int32 OUT SizeX;
int32 OUT SizeY;
GetViewportSize(SizeX, SizeY);
FVector2D CrosshairPositionOnScreen = FVector2D(SizeX * 0.5, SizeY * 0.5);

if (DeprojectScreenPositionToWorld(CrosshairPositionOnScreen.X,
		CrosshairPositionOnScreen.Y, Crosshair_3D_Location, Crosshair_3D_Direction)) {
        if (!GetKorniteOperatorPawn()) return;
        FVector OUT CameraLocation;GetKorniteOperatorPawn()->GetCameraLocation(CameraLocation);

        auto StartLocation = CameraLocation;
        auto EndLocation = StartLocation + Crosshair_3D_Direction * Range;
        DrawDebugLine(GetWorld(), StartLocation, EndLocation, FColor::Red, false, -1, 0, 10);
}

PlayerPawn - Fire Missile Function:

FVector Location = FVector(GetActorLocation().X,
		GetActorLocation().Y + 100, GetActorLocation().Z + 100); 
	FRotator Rotation = SceneCamera->GetComponentRotation();

	if (!ensure(ATGMissle_BP)) { return; }
        ATGMissle = GetWorld()->SpawnActor<AATGMissleActor>(
		ATGMissle_BP,
		Location,
		Rotation
        );

Actor - MissileActor lunched after firing by PlayerPawn - Tick Function:

auto PlayerController = Cast<AATGMPlayerController>(GetWorld()->GetFirstPlayerController());
if (!PlayerController) return;

auto CrosshairRotation = PlayerController->Crosshair_3D_Direction.Rotation();
auto ForwardVectorRotation = Missle->GetForwardVector().Rotation();
auto Rotation = CrosshairRotation - ForwardVectorRotation;

auto StartLocation = Missle->GetComponentLocation();
auto EndLocation = StartLocation + Missle->GetForwardVector() * 400000;
DrawDebugLine(GetWorld(), StartLocation, EndLocation, FColor::Green, false, -1, 0, 10);

if (!Missle) return;

Missle->AddRelativeRotation(Rotation);
auto Force = Missle->GetForwardVector() * Missle->GetMass() * Acceleration;
Missle->AddForce(DeltaTime * Force);

Any Help !!

Do you have your project hosted anywhere?

Thanks @DanM for your response :slight_smile:
My project in my local machine, with 6GB size.
What is the folder of the project should i share and upload ?

In Unreal,
File > Package Project > Zip Up Project
That will zip up only the necessary files.

Mr @DanM

The link of the project in mediafire
http://www.mediafire.com/file/4rlnqjfmfnw5ry5/ATGM.zip/file

What’s wrong with this exactly?

Mmmm !! :slight_smile:
1-Try to make the crosshair point close to target and not directly on it.
see the picture.
test

2-Then fire the missile with moving the crosshair directly to the target (cube).

3-ِ After the crosshair is guided at the target, the missile is supposed to hit the target, but unfortunately that not happened, why !!?

That’s just physics, it needs time to turn. You would probably need to add some logic to turn more sharply depending on the distance between the missile and target.

Privacy & Terms