The character won't move

I’ve written a code for movement and the character won’t move. I don’t know what caused it.
It was moving on Tuesday, but suddenly it’s not moving. There’s also a plug-in. I’ve also added it to Build.cs .
I don’t know the cause.
When I check one rotation through DisplayAll Player Controller Control Rotation, it rotates. Instead, the character doesn’t move.
I think it’s a problem with the code but I can’t find the cause…

#include "InputMappingContext.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"

void ACharacterPlayer::BeginPlay()
{
	Super::BeginPlay();

	APlayerController* PlayerController = CastChecked<APlayerController>(GetController());
	if (PlayerController)
	{
		if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
		{
			Subsystem->AddMappingContext(DefaultMappingContext, 0);
		}
	}

}

// Called to bind functionality to input
void ACharacterPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent);

	EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
	EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
	EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ALCharacterPlayer::Move);
	EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ACharacterPlayer::Look);
	EnhancedInputComponent->BindAction(AttackAction, ETriggerEvent::Triggered, this, &ACharacterPlayer::Attack);

}

void ACharacterPlayer::Move(const FInputActionValue& Value)
{
	FVector2D MovementVector = Value.Get<FVector2D>();	

	const FRotator Rotation = Controller->GetControlRotation();	
	const FRotator YawRotation(0, Rotation.Yaw, 0);


	const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

	AddMovementInput(ForwardDirection, MovementVector.X);
	AddMovementInput(RightDirection, MovementVector.Y);
}

void ACharacterPlayer::Look(const FInputActionValue& Value)
{
	FVector2D LookVector = Value.Get<FVector2D>();

	AddControllerYawInput(LookVector.X);
	AddControllerPitchInput(LookVector.Y);

}

I’d say check the Blueprint for the various things like actions and mappings assigned to the character is where your issue lies and not the C++.

Sometimes on restart, it loses your settings especially having used live coding.

Thank you so much for answering me first.

Live coding didn’t work well, so I deleted Binaries, Intermediate and did Generate Visual Studio… Even so, if I do live coding, I can’t detect that the code has changed.
Is that why…?

IMC has IAs registered well, what is the problem… Is there anything else to check?
I will attach the pictures about IA and IMC below.


IA_Jump

IA_Look

IA_Move


image
IMC_DefaultMappingContext

My character wasn’t moving, so I added ThirdPersonCharacter from Unreal itself. But this character was moving. So when I compared the blue print to the code of this character, it was pretty much the same.
But when I designated the class I made in ThirdPersonCharacter as the parent class, it didn’t move again.
That’s why I thought my code was a problem.

I suspect it could be. I’ve seen issues where setting the context wouldn’t work unless set in the player controller class. Technically, this is actually the correct place to set the player imput mapping context as it is for the player being controlled.

This is something you could try and I wouldn’t even bother with C++ here, simple blueprint on begin play would do the job.

I don’t understand… So do you mean I should set the code written below (blueprint) in Player Controller Class?
I made ACharacterPlayer BP, do I do it here? Or do I have to do it on the blue print set in PlayerController Class in GameMode?

void ACharacterPlayer::BeginPlay()
{
	Super::BeginPlay();

	APlayerController* PlayerController = CastChecked<APlayerController>(GetController());
	if (PlayerController)
	{
		if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
		{
			Subsystem->AddMappingContext(DefaultMappingContext, 0);
		}
	}

}


The picture above is from BP_ACharacterPlayer, but it was not executed.
I also set it up in the player controller class just in case it’s not here.

The picture above is from BP_PlayerController. This also doesn’t work… Maybe it’s my fault.

You need to tmset the player controller in the game mode

Are you saying I should do it with the quote in the blue print of game mode?
I’m sorry to ask you again.

image
This is how the Player Controller Class is set. When I did this, it didn’t move at all. (It used to move like this, but now it doesn’t move.)

Just in case, I also did the blue print as below. What do you mean by setting up the player controller? Do you mean setting up like the picture above?

What is tmset? I can’t speak English, so I’m using a translator to understand it. But tmset can’t be translated

It was a typing error, sorry. I was typing from my phone.
The blueprint you have there looks correct and if you add this into your Player Controller class on begin play, this should map correctly to the input. Without seeing the entire project however, I can’t be sure of the setup.

I would give this a try.

No problem. Thank you for your response.

The picture I sent you is a blue print of GameMode, is that correct? Or is it a blue print created by BP_LuckyPlayerController set in Player Controller Class?
But it still doesn’t move when I play it… I’ve annotated mapping IMCs written by CharacterPlayer.cpp for now.

image
When I play, I see the controller… Why isn’t it moving… Can I send my project?

It has to be in the player controller although the content is correct. If you move to the player controller it should work.

I didn’t understand what you mean I should be on player controller…
Are you saying that it should work if I code it on the player controller blueprint? I made a play by writing logic on the player controller blueprint.
But it’s not working… Am I missing something?

I am assuming the answer is yes but without access to your project, I can’t actually say.
So, to confirm. You are adding the Input Mapping context to the correct player as so:


This should be done in either the Player or in the Controller. If it is in both, comment out the C++ code only for the setting up of the context. Keep the binding code as this should still be present in the player

As a test, in the player blueprint, add something on one of your input actions to Print String - this will ensure your actions are correct and the context is working…

void ACharacterPlayer::BeginPlay()
{
	Super::BeginPlay();

	/*APlayerController* PlayerController = CastChecked<APlayerController>(GetController());
	if (PlayerController)
	{
		if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
		{
			Subsystem->AddMappingContext(DefaultMappingContext, 0);
		}
	}*/

}

The code has been annotated as above.

void ACharacterPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent);

	EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
	EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
	EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ACharacterPlayer::Move);
	EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ACharacterPlayer::Look);
	EnhancedInputComponent->BindAction(AttackAction, ETriggerEvent::Triggered, this, &ACharacterPlayer::Attack);

}

I left the binding code intact.


I worked on the blue print on the controller as shown in the picture above. I also added Print String for confirmation.

I tried playing it and it still doesn’t work… Instead, if you play, OK comes to the viewport. But it doesn’t move.

Can you jump? I think the move binding is wrong. I just looked at the code again and you have a mistake in the class name.

ALCharscterPlayer
I missed this before and thought that is strange. Give that a try.

No jump. I randomly deleted the class name and uploaded the code haha…
I will send you the original code.

void ALuckyCharacterPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent);

	EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
	EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
	EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ALuckyCharacterPlayer::Move);
	EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ALuckyCharacterPlayer::Look);
	EnhancedInputComponent->BindAction(AttackAction, ETriggerEvent::Triggered, this, &ALuckyCharacterPlayer::Attack);

}

void ALuckyCharacterPlayer::Move(const FInputActionValue& Value)
{
	FVector2D MovementVector = Value.Get<FVector2D>();	

	const FRotator Rotation = Controller->GetControlRotation();
	const FRotator YawRotation(0, Rotation.Yaw, 0);

	const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

	AddMovementInput(ForwardDirection, MovementVector.X);
	AddMovementInput(RightDirection, MovementVector.Y);
}

void ALuckyCharacterPlayer::Look(const FInputActionValue& Value)
{
	FVector2D LookVector = Value.Get<FVector2D>();

	AddControllerYawInput(LookVector.X);
	AddControllerPitchInput(LookVector.Y);

}

void ALuckyCharacterPlayer::Attack()
{
	AttackPress();
}

Ok. In the character blueprint, right click and add IA_Move or whatever the action is called. Then add a print string (possibly print the value from the input.)

Okay. So, is it right to do the character blue print with the picture? IA_Attack also did it with the picture.

I ran it once. Similarly, the character doesn’t move, but when you press the keyboard, IA_Move appears in the viewport, when you rotate the mouse, IA_Look appears, when you press space, IA_Jump appears, and when you click the mouse, IA_Attack appears.

Strangely, the character doesn’t move.
The result of print string is shown in the viewport, and the log appears when you press the input key in the output log.

LogBlueprintUserMessages: [BP_LuckyCharacter_C_0] IA_Move
LogBlueprintUserMessages: [BP_LuckyCharacter_C_0] IA_Move
LogBlueprintUserMessages: [BP_LuckyCharacter_C_0] IA_Jump

Then there is something not quite right in the C++ of the character. It means your input is working but for some reason the C++ isn’t binding.

At this point I am not sure what to suggest. Perhaps create a second blueprint from the C++ class and check that as a player. This does occasionally work.

Other than that, add log statements into your code and see what is being executed and what isn’t. For starters, add into the SetupPlayerInput.

void ALuckyCharacterPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	UE_LOG(LogTemp, Error, TEXT("SetupPlayerInputComponent"));

	UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent);
	if (EnhancedInputComponent)
	{
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
		EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ALuckyCharacterPlayer::Move);
		EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ALuckyCharacterPlayer::Look);
		EnhancedInputComponent->BindAction(AttackAction, ETriggerEvent::Triggered, this, &ALuckyCharacterPlayer::Attack);
		UE_LOG(LogTemp, Display, TEXT("Use EnhancedInputComponent"));
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("Not EnhancedInputComponent"));
	}
}

void ALuckyCharacterPlayer::Move(const FInputActionValue& Value)
{
	FVector2D MovementVector = Value.Get<FVector2D>();	
	const FRotator Rotation = Controller->GetControlRotation();	
	const FRotator YawRotation(0, Rotation.Yaw, 0);

	const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

	AddMovementInput(ForwardDirection, MovementVector.X);
	AddMovementInput(RightDirection, MovementVector.Y);
	UE_LOG(LogTemp, Display, TEXT("Move"));
}

void ALuckyCharacterPlayer::Look(const FInputActionValue& Value)
{
	FVector2D LookVector = Value.Get<FVector2D>();

	AddControllerYawInput(LookVector.X);
	AddControllerPitchInput(LookVector.Y);
	UE_LOG(LogTemp, Display, TEXT("Rotation"));
}

void ALuckyCharacterPlayer::Attack()
{
	UE_LOG(LogTemp, Display, TEXT("Attack"));
	AttackPress();
}

Once played, the SetupPlayerInputComponent log appears.
But Move, Rotation logs don’t show up at all. But Attack logs do.
There’s something wrong with that.

Privacy & Terms