C++ Character Class & LineTrace

Hello everybody,

I’m currently working on something involving Line tracing. I had created a Blueprint Character and assigned it the class that I wanted it to derive from. Unfortunately in C++ my HitResult does not seem to pick up the BP Character when he is out in the world. Likely because C++ doesn’t recognize BP.

So the alternative that I wanted to perform was abit different. I want to create a full character, assigning it’s mesh ect via C++. What is the alternative and code required to setup a basic character actor with C++? I’ve already got my skeletal mesh ready to go all it needs to do is be assigned.

So what exactly is the basic code in a character constructor to emulating a BP character? Thanks a bunch.

If you are talking about the main character you move around,

Put this in your GameMode.cpp

static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("Blueprint'/Game/Blueprints/ShipBp'"));//
	if (PlayerPawnBPClass.Class != NULL)
	{
		DefaultPawnClass = PlayerPawnBPClass.Class;
	}

of course the TEXT would change to whatever your blueprint is and would be changed to the type of blueprint.

If you don’t know how to get the bp address you right click on the bp and Click Copy Reference, you can then paste it in the TEXT area

I use in my character .h file

UFUNCTION()
		void OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

in the .cpp

CollisionComponent->OnComponentHit.AddDynamic(this, &AMyCharacter::OnHit);
and

void AMyCharacter::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{

}

No, no. This is for my AI that i’ve created. I’ve made a character blueprint for them; assigned their mesh and made a parent class that handles all of it’s behavior.

The problem I am having however is that my main character, the player character, when I go to perform a line trace onto this BP it doesn’t work. I figured in the long run it would be easier to simply make an actor that inherits from ACharacter and my AIController class.

The issue I am having now though is well… I want to give it the same setup as my player BP without it being a BP. The constructor requires some basic things.

What you posted up is in regards to an OnHit call, or a simple collision call. It doesn’t really answer my question but thank you.

I suppose a better question would be… how would I get a line trace from a class inheriting from ACharacter?

Character Line Trace needed this! It required ECC_Pawn. While the ACharacter class needed something similar. The class needed to be set to ECC_Pawn.

if (GetWorld()->LineTraceSingle(HitResult, StartTrace, EndTrace, ECC_Pawn, TraceParams))

///////////////////////////////////// MyChar ^
//////////////////////////////
///////////////////////////////////// ACharacter Class

	FORCEINLINE void SetupSMComponentsWithCollision(USkeletalMeshComponent* Comp) // Default is UStaticMeshComponent
	{
		if (!Comp)
		{
			return;
		}
		//~~~~~~~~

		Comp->bOwnerNoSee = false;
		Comp->bCastDynamicShadow = true;
		Comp->CastShadow = true;
		Comp->BodyInstance.SetObjectType(ECC_Pawn);
		Comp->BodyInstance.SetResponseToAllChannels(ECR_Ignore);
		Comp->BodyInstance.SetResponseToChannel(ECC_WorldStatic, ECR_Block);
		Comp->BodyInstance.SetResponseToChannel(ECC_WorldDynamic, ECR_Block);
		Comp->BodyInstance.SetResponseToChannel(ECC_Pawn, ECR_Block);
		Comp->SetHiddenInGame(false);
		Comp->SetRelativeScale3D(FVector(2.0, 2.0, 2.0));
		//Comp->SetRelativeLocation(FVector(100, 100, 100), false); // Changes position
	}

I want to add that this works with Blueprint Characters as well.