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"

How can I get my pawn moving towards its forward direction

Hey,

I'm trying to make a pawn that will move towards the forward direction whenever the player is pressing the input to do so. Currently I can make it move up and down the X axis, however whenever the player rotates the object using the mouse it will continue to only move along the X-Axis, rather than moving forwards towards the direction it is facing. I've posted the code I have below #include "PlayerCharacter.h" #include "Cplusplusfirsttry.h" #include "Camera/CameraComponent.h" #include "Components/InputComponent.h" #include "Components/StaticMeshComponent.h" #include "GameFramework/PlayerController.h" #include "Engine/LocalPlayer.h" #include "GameFramework/DefaultPawn.h" #include "GameFramework/SpringArmComponent.h"

 float InputValue;
 // Sets default values
 APlayerCharacter::APlayerCharacter()
 {
      // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
     AutoPossessPlayer = EAutoReceiveInput::Player0;
     
     //create a camera and a mesh, then set the root component to the mesh
     PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
     PlayerMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
     RootComponent = PlayerMesh;
     //create springarm and attach it and mesht to root component. attach camera to springarm
     CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm")); //creating a springarm for the camera
     CameraSpringArm->SetupAttachment(RootComponent);
     CameraSpringArm->TargetArmLength = 0.0f; //how far from the spring arm the camera is. As it is first person this will be 0
     CameraSpringArm->SetWorldRotation(FRotator(0.0f, 0.0f, 0.0f)); //initialising roation. should start directly straight
     
     PlayerCamera->SetupAttachment(CameraSpringArm, USpringArmComponent::SocketName); //attaches camera to end of spring arm
     PlayerSpeed = 250.0f;
     
 }
 
 
 // Called when the game starts or when spawned
 void APlayerCharacter::BeginPlay()
 {
     Super::BeginPlay();
     
 }
 
 // Called every frame
 void APlayerCharacter::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
     //handles movement based on the MoveX and MoveY axes
     if (!CurrentVelocity.IsZero())
     {
         FVector Direction = GetActorForwardVector();
         FVector NewLocation = (GetActorLocation() + (CurrentVelocity * DeltaTime));
         SetActorLocation(NewLocation);
     }
     FRotator NewX = GetActorRotation(); //allows x axis camera to rotate with character
     NewX.Yaw += MouseInput.X;
     SetActorRotation(NewX);
 
     FRotator NewY = CameraSpringArm->GetComponentRotation(); //means character will not rotate on Y axis when looking up
     NewY.Pitch = FMath::Clamp(NewY.Pitch += MouseInput.Y, -60.f, 60.f);
     CameraSpringArm->SetWorldRotation(NewY);
 }
 
 // Called to bind functionality to input
 void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
 {
     Super::SetupPlayerInputComponent(PlayerInputComponent);
     //respond to the MoveX and MoveY commands
     PlayerInputComponent->BindAxis("MoveForward", this, &APlayerCharacter::Move_Forward);
     PlayerInputComponent->BindAxis("MoveSide", this, &APlayerCharacter::Move_Side);
     //respond to mouse movement
     PlayerInputComponent->BindAxis("MouseX", this, &APlayerCharacter::MouseX);
     PlayerInputComponent->BindAxis("MouseY", this, &APlayerCharacter::MouseY);
     
 }
 void APlayerCharacter::Move_Forward(float Input)
 {
     
     
     CurrentVelocity.X = FMath::Clamp(Input, -1.0f, 1.0f) * PlayerSpeed;
     
 }
 void APlayerCharacter::Move_Side(float Input)
 {
  
     
 }
 void APlayerCharacter::MouseX(float AxisValue)
 {
     MouseInput.X = AxisValue;
 }
 void APlayerCharacter::MouseY(float AxisValue)
 {
     MouseInput.Y = AxisValue;
 }
 
Product Version: UE 4.20
Tags:
more ▼

asked Jan 17 '19 at 10:54 PM in C++ Programming

avatar image

Sirhaddock
1 1

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Your problem is the combination of this line:

 CurrentVelocity.X = FMath::Clamp(Input, -1.0f, 1.0f) * PlayerSpeed;

...with these lines:

 FVector Direction = GetActorForwardVector();
 FVector NewLocation = (GetActorLocation() + (CurrentVelocity * DeltaTime));
 SetActorLocation(NewLocation);

You seem to be almost there, but your code doesn't appear to make use of your character's forward vector as intended. (Even though you store the forward vector in a variable, the variable isn't used!)

You should be able to get your code working to some extent by just incorporating your Direction vector. Maybe something like:

 FVector Direction = GetActorForwardVector();
 FVector NewLocation = (GetActorLocation() + (CurrentVelocity.X * Direction * DeltaTime));
 SetActorLocation(NewLocation);

But, even with that change, I wonder whether you shouldn't consider a slightly different (and simpler) approach.

If you haven't already, I suggest taking a look at the C++ code that is generated with the First Person template, specifically the Player Controller and Pawn classes. Even if you're not making a first-person game, the code examples from that template show how to neatly use inputs to call very small functions that handle movement in all directions and rotation of the pawn using the mouse. I think you might find it useful to adapt some of the simplicity of that template code for your character class. (Just a suggestion! Totally up to you.)

more ▼

answered Jan 18 '19 at 04:49 AM

avatar image

the_batch
1.1k 4 5 8

avatar image Sirhaddock Jan 18 '19 at 02:45 PM

Thanks a bunch dude, that worked great. I feel kind of silly for not getting it now lol.

(comments are locked)
10|2000 characters needed characters left
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