Forward vector help.

I have never had this problem before so i cant figure out why its happening.

i am trying to get the forward vector from the player camera this is the result i had. Screenshot - e1d04568dc1c8f07ddc7fedd1492a87a - Gyazo

As you can see the line trace is going upwards over time and not straight is there a better way of achieving this or as i doing something wrong?

This is the code i am using



	FVector StartTrace = Character->GetFirstPersonCameraComponent()->GetSocketLocation("");
	FVector ForwardTrace = Character->GetFirstPersonCameraComponent()->GetForwardVector();
	FVector EndTrace = StartTrace + ForwardTrace * WeaponStats.MaxBulletRange;


Try using the Component location instead of a empty socket location. My guess is GetSocketLocation("") is returning a zero vector (0,0,0), causing an issue with the trace start location.



	FVector StartTrace = Character->GetFirstPersonCameraComponent()->GetComponentLocation();
	FVector ForwardTrace = Character->GetFirstPersonCameraComponent()->GetForwardVector();
	FVector EndTrace = StartTrace + ForwardTrace * WeaponStats.MaxBulletRange;


Doesn’t change.
The ray-cast still goes upwards this is the view when shot.

You don’t want to use Component Location if you’re trying to get the camera location. There’s a helper method called GetPlayerViewPoint that will get you the camera position/rotation if using a first person camera.

I have tested this on 4.14.3 and 4.16.2 and both versions give me the same outcome.
I have tried two projects on 4.14.3 and two projects on 4.16.2 and it is always the same.

I have also tried to recreate this in blueprint and it gives the same outcome on that as well.

I have tested 3 pieces of code and both give me the same result.

I used the last piece of code for ages and it worked perfectly but now this problem is now only starting to occur



	FVector CameraLocation;
	FRotator CameraRotation;

	World->GetFirstPlayerController()->GetPlayerViewPoint(CameraLocation, CameraRotation);

	FVector ForwardTrace = GetFirstPersonCameraComponent()->GetForwardVector();
	FVector EndTrace = CameraLocation + ForwardTrace * 5000;




	FVector StartTrace = GetFirstPersonCameraComponent()->GetComponentLocation();
	FVector ForwardTrace = GetFirstPersonCameraComponent()->GetForwardVector();
	FVector EndTrace = StartTrace + ForwardTrace * 5000;




	FVector StartTrace = GetFirstPersonCameraComponent()->GetSocketLocation("");
	FVector ForwardTrace = GetFirstPersonCameraComponent()->GetForwardVector();
	FVector EndTrace = StartTrace + ForwardTrace * 5000;


When the ray-cast starts it is in a straight line from the camera, but overtime the ray-cast starts to arc upwards i dont know what is making this happen.

The image is blurry but you can still see what is happening, The blue line is how the ray-cast is meant to look. The red line is the ray-cast.

38b4374e0c096f8e42033d9645717cd5.png

Sorry, I didn’t fully understand your goal.

Here is how I would setup a trace for a FPS weapon tracing from the center of the camera, outwards:




void AFPSExample_Character::Shoot( )
{
    APlayerController *MyController = Cast<APlayerController>(GetController( ));
    check(MyController);
    
    FVector CameraLocation;
    FRotator CameraRotation;
    
    MyController->GetPlayerViewPoint(CameraLocation, CameraRotation);
    
    check(CameraLocation != FVector::ZeroVector);
    
    FVector StartTraceLocation = CameraLocation;
    FVector EndTraceLocation = CameraLocation + (CameraRotation.Vector( ) * 65538.f);
    
    FHitResult Hit;
    FCollisionQueryParams TraceParams( FName( TEXT("Teleport Trace")), true, this );
    
    if( GetWorld( )->LineTraceSingleByChannel(Hit, StartTraceLocation,EndTraceLocation, 
                                              ECC_WorldStatic, TraceParams))
    {   
        UKismetSystemLibrary::DrawDebugLine(GetWorld( ), StartTraceLocation, EndTraceLocation, FLinearColor::Red, 12.f, 3.f);    
        UKismetSystemLibrary::DrawDebugSphere(GetWorld( ), Hit.ImpactPoint, 24.f, 8, FLinearColor::Yellow, 12.f, 3.f);
    }
    else
    {
        UE_LOG(LogTemp, Warning, TEXT("Shoot trace failed!"));   

        // Case where player was probably aiming into the sky and didnt hit anything "world static".
        // If needed, can use EndTraceLocation as point where trace "hit" as it is the absolute farthest distance gun can fire.
    }
}


This traces from the center of the camera, using the cameras rotation as direction, and gets a point 65537 units forward. If trace is hit, it returns true. You can then use Hit.ImpactPoint as the vector in the world as to what is hit. The FHitResult also holds any Actors hit, such as players.

Why not just zero out the Z component of the forward vector and renormalize it?

The Player Camera Manager doesn’t have a forward vector that represents the camera. It’s an actor.

You need to use ‘Get Player Viewpoint’, and get the forward vector from the rotator.