x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

AddControllerYawInput is adding undesired movement

Hello,

I have an ALPlayerController class that inherits from APlayerController, in which I am doing the usual input actions such as moving the character forward, strafing and turning (by calling AddControllerYawInput).

Thing is, I also wanted to set it so that if both the two mouse buttons were pressed at the same time, it would be equivalent to moving forward. The way I did it was setting two booleans and checking them in the Tick function, and it works fine, but...

When I press the two mouth buttons + S, the character doesn't move (works fine), but when i also press the A or D key to turn, it suddenly gets moving instead of just turning while staying still. In fact, the resulting movement is pretty much equivalent to moving forward + turning, but with a different orientation. Supposedly the forward and backward movement gets zeroed as they are opposed, why is there any movement when i call the AddControllerYawInput() ??

Note: The Acceleration displayed in the Tick() function is not zero when this happens.

The code for the controller:

 #include "Lynaeorium.h"
 #include "LPlayerController.h"
 #include <Lynaeorium/Components/NewPlayerMovement.h>
 
 
 ALPlayerController::ALPlayerController()
 {
     bCameraFreeLookOn, bCameraRotationOn = false;
 }
 
 
 void ALPlayerController::Tick(float DeltaSeconds)
 {
     Super::Tick(DeltaSeconds);
 
     if (bCameraRotationOn && bCameraFreeLookOn) 
         GetPawn()->AddMovementInput(GetPawn()->GetActorForwardVector(), 1.f);
 
     if (GEngine) GEngine->AddOnScreenDebugMessage(0, 2.f, FColor::Magenta, GetCharacter()->GetCharacterMovement()->GetCurrentAcceleration().ToString());
 }
 
 
 void ALPlayerController::ActivateCameraFreeLook()
 {
     bCameraFreeLookOn = true;
 }
 
 
 void ALPlayerController::DeactivateCameraFreeLook()
 {
     bCameraFreeLookOn = false;
 }
 
 
 void ALPlayerController::ActivateCameraRotation()
 {
     bCameraRotationOn = true;
 }
 
 
 void ALPlayerController::DeactivateCameraRotation()
 {
     bCameraRotationOn = false;
 }
 
 
 void ALPlayerController::Jump()
 {
     if (PlayerMovementIsValid()) PlayerMovement->SetJump();
 }
 
 
 void ALPlayerController::SetForwardMovement(float AxisValue)
 {
     GetPawn()->AddMovementInput(GetPawn()->GetActorForwardVector(), AxisValue);
 }
 
 
 void ALPlayerController::SetStrafeMovement(float AxisValue)
 {
     GetPawn()->AddMovementInput(GetPawn()->GetActorRightVector(), AxisValue);
 }
 
 
 void ALPlayerController::SetTurnMovement(float AxisValue)
 {
     GetPawn()->AddControllerYawInput(FMath::Clamp(AxisValue, -1.f, 1.f));
 }
 
 
 bool ALPlayerController::PlayerMovementIsValid()
 {
     if (PlayerMovement) return true;
 
     PlayerMovement = Cast<UNewPlayerMovement>(GetCharacter()->GetCharacterMovement());
     return PlayerMovement ? true : false;
 }
 
 
 void ALPlayerController::SetupInputComponent()
 {
     Super::SetupInputComponent();
 
     InputComponent->BindAction("CameraFreeLook", IE_Pressed, this, &ALPlayerController::ActivateCameraFreeLook);
     InputComponent->BindAction("CameraFreeLook", IE_Released, this, &ALPlayerController::DeactivateCameraFreeLook);
     InputComponent->BindAction("CameraRotation", IE_Pressed, this, &ALPlayerController::ActivateCameraRotation);
     InputComponent->BindAction("CameraRotation", IE_Released, this, &ALPlayerController::DeactivateCameraRotation);
     InputComponent->BindAction("Jump", IE_Pressed, this, &ALPlayerController::Jump);
 
     InputComponent->BindAxis("Forward", this, &ALPlayerController::SetForwardMovement);
     InputComponent->BindAxis("Strafe", this, &ALPlayerController::SetStrafeMovement);
     InputComponent->BindAxis("Turn", this, &ALPlayerController::SetTurnMovement);
 }



Product Version: UE 4.9
Tags:
more ▼

asked Sep 11 '15 at 03:29 AM in C++ Programming

avatar image

Suui
141 3 6 20

avatar image Suui Sep 11 '15 at 03:50 AM

This problem has to deal with the AddMovementInput() being called in the Tick function. I tested removing it, and checking the pressed mouse buttons in a function binded to an Axis, which turned out to work fine.

I found a workaround: Having an Axis Mapping for the RightMouseButton, checking both mouse buttons there and applying the movement inside it. But this is pretty dirty and will probably bring some problems if I try doing other things with the mouse buttons.

Is there a function that gets called every time the Axis Mappings are checked, without the need to have a key pressed?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

I found the function that gets called for the Axis readings, which is in the PlayerController.h file and protected:

virtual void ProcessPlayerInput(const float DeltaTime, const bool bGamePaused);

So I overrided it and set it up in the cpp, and now everything works as intended! I'll leave the answer in case someone runs into this same problem.

Here is the overrided function:

 void ALPlayerController::ProcessPlayerInput(const float DeltaTime, const bool bGamePaused)
 {
     Super::ProcessPlayerInput(DeltaTime, bGamePaused);
 
     if (bCameraRotationOn && bCameraFreeLookOn)
         GetPawn()->AddMovementInput(GetPawn()->GetActorForwardVector(), 1.f);
 }

more ▼

answered Sep 11 '15 at 04:21 AM

avatar image

Suui
141 3 6 20

avatar image Timbo Oct 06 '15 at 04:32 AM

Had a similar problem. thx

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question