Download

Pawn Camera not Following Child Actor

Hi,

I’m currently working on a space ship game.

and I’m doing something weird for my pawn :), my paw has a ChildActorComponent for the ship, this child will be contain a Ship actor (The current ship for the pawn, the player can change ship during the game, but the control will be the same so I thinked to do thing like that, change the ship on the pawn and get the ship properties across the PC and the Pawn)

actually it’s work well, but my Pawn camera isn’t following my ship. The ship is moving, rotating well.

this is my c++ code for my Pawn :



// Fill out your copyright notice in the Description page of Project Settings.

#include "spaceShip.h"
#include "SS_BasePawn.h"


//--------------------------------------
// Sets default values
***_BasePawn::***_BasePawn(const class FObjectInitializer& PCIP) : Super(PCIP)
{
	Ship = PCIP.CreateDefaultSubobject<UChildActorComponent>(this, TEXT("Ship"));
	Ship->ChildActorClass = AShips::StaticClass();
	RootComponent = Ship;

	// Create a camera boom
	SpringCamera = PCIP.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("SpringCamera"));
	SpringCamera->AttachTo(RootComponent);
	SpringCamera->TargetOffset = FVector(0.f, 0.f, 0.f);
	SpringCamera->SetRelativeLocation(FVector(0.f, 0.f, 0.f));
	SpringCamera->SetRelativeRotation(FRotator(0.f, 0.f, 0.f));
	SpringCamera->TargetArmLength = 800.0f;
	SpringCamera->bUsePawnControlRotation = false;
	SpringCamera->bEnableCameraLag = true;
	SpringCamera->CameraLagSpeed = 5.f;
	SpringCamera->CameraRotationLagSpeed = 5.f;
	SpringCamera->CameraLagMaxDistance = 5.f;

	// Create a follow camera
	Camera = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("Camera"));
	Camera->AttachTo(SpringCamera, USpringArmComponent::SocketName);
	Camera->bUsePawnControlRotation = false;
	Camera->FieldOfView = 90.f;
	Camera->SetRelativeLocation(FVector(0.f, 0.f, 200.f));

	BumpEmitter = PCIP.CreateDefaultSubobject<UParticleSystem>(this, TEXT("BumpEmitter"));

	bReplicates = true;
	PrimaryActorTick.bCanEverTick = true;

	MoveForwardScale = 40.f;
	MoveRightScale = 40.f;
	RollScale = 5.f;
	LookUpScale = 5.f;
	LookRightScale = 5.f;
}

//--------------------------------------
// Begin play
void ***_BasePawn::BeginPlay()
{
	Super::BeginPlay();
	Ship->OnComponentCreated();
}

//--------------------------------------
// Tick
void ***_BasePawn::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

	if (HasAuthority()) {
		ClientSmoothTransform(GetTransform());
	}
}

void ***_BasePawn::ClientSmoothTransform_Implementation(FTransform _transform) {
	SetActorTransform(_transform, false);
}

bool ***_BasePawn::ClientSmoothTransform_Validate(FTransform _transform) {
	return true;
}

//--------------------------------------
// Input Setup
void ***_BasePawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	check(InputComponent);

	InputComponent->BindAxis("MoveForward", this, &***_BasePawn::MoveForward);
	InputComponent->BindAxis("MoveRight", this, &***_BasePawn::MoveRight);
	InputComponent->BindAxis("Roll", this, &***_BasePawn::Roll);
	InputComponent->BindAxis("LookUp", this, &***_BasePawn::LookUp);
	InputComponent->BindAxis("LookRight", this, &***_BasePawn::LookRight);

	InputComponent->BindAction("BumpAction", IE_Pressed, this, &***_BasePawn::BumpAction);

	Super::SetupPlayerInputComponent(InputComponent);

}

//--------------------------------------
// Move Forward
void ***_BasePawn::MoveForward(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f)) {
		ServerMoveForward(Value);
	}
}

void ***_BasePawn::ServerMoveForward_Implementation(float value) {
	if (HasAuthority()) {
		Cast<AShips>(Ship->ChildActor)->ShipMesh->SetAllPhysicsLinearVelocity((value * MoveForwardScale) * GetActorForwardVector(), true);
	}
}

bool ***_BasePawn::ServerMoveForward_Validate(float value) {
	return true;
}

//--------------------------------------
// Move Right
void ***_BasePawn::MoveRight(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f)) {
		ServerMoveRight(Value);
	}
}

void ***_BasePawn::ServerMoveRight_Implementation(float value) {
	if (HasAuthority()) {
		Cast<AShips>(Ship->ChildActor)->ShipMesh->SetAllPhysicsLinearVelocity((value * MoveRightScale) * GetActorRightVector(), true);
	}
}

bool ***_BasePawn::ServerMoveRight_Validate(float value) {
	return true;
}

//--------------------------------------
// Roll
void ***_BasePawn::Roll(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f)) {
		ServerRoll(Value);
	}
}

void ***_BasePawn::ServerRoll_Implementation(float value) {
	if (HasAuthority()) {
		Cast<AShips>(Ship->ChildActor)->ShipMesh->SetAllPhysicsAngularVelocity((value * RollScale) * GetActorForwardVector(), true);
	}
}

bool ***_BasePawn::ServerRoll_Validate(float value) {
	return true;
}

//--------------------------------------
// Look Up
void ***_BasePawn::LookUp(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f)) {
		ServerLookUp(Value);
	}
}

void ***_BasePawn::ServerLookUp_Implementation(float value) {
	if (HasAuthority()) {
		Cast<AShips>(Ship->ChildActor)->ShipMesh->SetAllPhysicsAngularVelocity((value * LookUpScale) * GetActorRightVector(), true);
	}
}

bool ***_BasePawn::ServerLookUp_Validate(float value) {
	return true;
}

//--------------------------------------
// Look Right
void ***_BasePawn::LookRight(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f))
	{
		ServerLookRight(Value);
	}
}

void ***_BasePawn::ServerLookRight_Implementation(float value) {
	if (HasAuthority()) {
		Cast<AShips>(Ship->ChildActor)->ShipMesh->SetAllPhysicsAngularVelocity((value * LookRightScale) * GetActorUpVector(), true);
	}
}

bool ***_BasePawn::ServerLookRight_Validate(float value) {
	return true;
}

//--------------------------------------
// Bump Action
void ***_BasePawn::BumpAction()
{
	if (Controller != NULL)
	{
		ServerBump(Cast<AShips>(Ship->ChildActor)->GetActorLocation(), Cast<AShips>(Ship->ChildActor)->BumpPower, Cast<AShips>(Ship->ChildActor)->BumpRadius, this);
	}
}

void ***_BasePawn::ServerBump_Implementation(FVector BumpCenter, float BumpPower, float BumpRadius, APawn *BumpInstigator) {
	TArray<AActor*> SS_BasePawnArray;
	UGameplayStatics::GetAllActorsOfClass(this->GetWorld(), ***_BasePawn::StaticClass(), SS_BasePawnArray);
	for (int i = 0; i < SS_BasePawnArray.Num(); i++) {
		if (Cast<***_BasePawn>(SS_BasePawnArray*) != BumpInstigator)
			Cast<***_BasePawn>(SS_BasePawnArray*)->GetBumped(BumpCenter, BumpPower, BumpRadius);
	}
	ShowBump(BumpCenter);
}

bool ***_BasePawn::ServerBump_Validate(FVector BumpCenter, float BumpPower, float BumpRadius, APawn *BumpInstigator) {
	return true;
}

void ***_BasePawn::GetBumped(FVector BumpCenter, float BumpPower, float BumpRadius){
	Cast<AShips>(Ship->ChildActor)->ShipMesh->AddRadialImpulse(BumpCenter, BumpRadius, BumpPower, ERadialImpulseFalloff::RIF_Constant, false);
}

void ***_BasePawn::ShowBump_Implementation(FVector BumpLocation) {
	UGameplayStatics::SpawnEmitterAtLocation(this->GetWorld(), BumpEmitter, BumpLocation, FRotator(0.f, 0.f, 0.f), true);
}


and the c++ code for the shipBase class :



// Fill out your copyright notice in the Description page of Project Settings.

#include "spaceShip.h"
#include "Classes/Ships/Ships.h"


//--------------------------------------
// Sets default values
AShips::AShips(const class FObjectInitializer& PCIP) : Super(PCIP)
{
	ShipMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("ShipMesh"));
	ShipMesh->bReceivesDecals = true;
	ShipMesh->CastShadow = true;
	ShipMesh->SetCollisionObjectType(ECC_WorldDynamic);
	ShipMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
	ShipMesh->SetCollisionResponseToAllChannels(ECR_Ignore);
	ShipMesh->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);
	ShipMesh->SetSimulatePhysics(true);
	ShipMesh->SetAngularDamping(1.f);
	ShipMesh->SetLinearDamping(1.f);
	ShipMesh->SetEnableGravity(true);
	RootComponent = ShipMesh;

	PrimaryActorTick.bCanEverTick = true;
}

//--------------------------------------
// Begin play
void AShips::BeginPlay()
{
	Super::BeginPlay();
}

//--------------------------------------
// Tick
void AShips::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
}



By next the ship clas will contain more stuff, like weapon slot, shield slot, …

Have you any Idea on how to correct that ?