Firstly I am using two pointers of UPlayerInputComponent which are
UInputComponent* mp_player_input_component;
UInputComponent* mp_car_input_component;
A character which is firstly binded its axis
void AMy_character::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
mp_player_input_component->BindAxis(FName(TEXT("Up_down")), this, &AMy_character::Move_up_down);
mp_player_input_component->BindAxis(FName(TEXT("Left_right")), this, &AMy_character::Move_left_right);
mp_player_input_component->BindAxis(FName(TEXT("Look_up")), this, &AMy_character::Look_up);
mp_player_input_component->BindAxis(FName(TEXT("Turn")), this, &AMy_character::Turn);
if (InputComponent)
InputComponent = mp_player_input_component;
}
Then when a player press the interaction key which will be F in this case, it should possess the object which is collided and change the possession, but when changed possession the InputComponent won’t reach the possessed pawn input component
auto controller = UGameplayStatics::GetPlayerController(GetWorld(), 0);
m_current_vehicle->Init_input_controller(mp_car_input_component);
InputComponent = mp_car_input_component;
controller->UnPossess();
controller->Possess(Cast<APawn>(m_current_vehicle));
m_current_vehicle->p_widget_component->SetVisibility(false);
m_current_vehicle->p_spring_arm->SetRelativeTransform(p_spring_arm->GetRelativeTransform());
m_current_vehicle->p_camera->SetRelativeTransform(p_camera->GetRelativeTransform());
And the functions used in the possessed pawn are
void ABase_interaction_pawn::Init_input_controller(UInputComponent* PlayerInputComponent)
{
PlayerInputComponent->BindAxis(TEXT("Up_down"), this, &ABase_interaction_pawn::Move_up_down);
PlayerInputComponent->BindAxis(TEXT("Left_right"), this, &ABase_interaction_pawn::Move_left_right);
}
void ABase_interaction_pawn::Move_up_down(float _value)
{
GEngine->AddOnScreenDebugMessage(0, 1.f, FColor::Green, FString("Pressed key vehicle"));
AddMovementInput(GetActorForwardVector() * _value);
}
void ABase_interaction_pawn::Move_left_right(float _value)
{
AddMovementInput(GetActorRightVector() * _value);
}