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"

Move pawn along forward/right vectors

I have created a pawn that moves along the x and y axis, and rotates left and right. However, when I rotate, the pawn still moves along the original x and y axis, so it doesn't look right when it moves after rotating. How would I use the pawns forward and right vectors to correct the movement so that the pawn still moves correctly?

Product Version: UE 4.16
Tags:
more ▼

asked Dec 02 '17 at 09:01 PM in C++ Programming

avatar image

MartyBro
11 4 5 6

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

1 answer: sort voted first
more ▼

answered Dec 03 '17 at 07:39 AM

avatar image

Firefly74
1.3k 13 15 30

avatar image MartyBro Dec 03 '17 at 01:47 PM

Unfortunately, when I tried to implement this, it didn't work. I don't know why, but apparently this approach doesn't work for me. Thank you though.

avatar image Firefly74 Dec 03 '17 at 05:38 PM

Then maybe your rotation code is doing something wrong, do you rotate controler ? Pawn ? Camera ?

avatar image MartyBro Dec 03 '17 at 05:41 PM

I'm rotating a pawn. I have a variable which gets the pawns rotation and then adds/subtracts depending on the buttons pressed. However, the vectors do not rotate and I cannot figure out how to get them to rotate.

avatar image Firefly74 Dec 03 '17 at 06:25 PM

Could you share:

-your moving code + name of method + class it is in

-your turning code + name of method+ class it is in

avatar image MartyBro Dec 03 '17 at 06:29 PM

void ADuck::Tick(float DeltaTime) { Super::Tick(DeltaTime);

 // Turning left and right
 FRotator TurningValue = GetActorRotation();
 if (bTurningLeft)
 {
     TurningValue.Yaw -= 1;
 }
 if (bTurningRight)
 {
     TurningValue.Yaw += 1;
 }
 SetActorRotation(TurningValue);


 // Move in all directions, but restrict movement to set distance so duck stays on water
 if (!Velocity.IsZero())
 {
     FVector NewDuckLocation = GetActorLocation() + (Velocity * DeltaTime);
     NewDuckLocation.X = FMath::Clamp(int(NewDuckLocation.X), 150, 665);
     NewDuckLocation.Y = FMath::Clamp(int(NewDuckLocation.Y), 120, 675);
     SetActorLocation(NewDuckLocation);
 }

}

void ADuck::MoveForwardBackward(float AxisValue) {

 // Move along the x-axis using controls set in the project settings
 Velocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 500.0f;

}

void ADuck::MoveLeftRight(float AxisValue) { // Move along the y-axis using controls set in the project settings Velocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 500.0f; }

void ADuck::LeftTurn() { bTurningLeft = true; }

void ADuck::LeftStop() { bTurningLeft = false; }

void ADuck::RightTurn() { bTurningRight = true; }

void ADuck::RightStop() { bTurningRight = false; }

 // Input functions for movement, turning 
 void MoveForwardBackward(float AxisValue);
 void MoveLeftRight(float AxisValue);
 void LeftTurn();
 void LeftStop();
 void RightTurn();
 void RightStop();


 // Input variables for movement, turning
 FVector Velocity;
 bool bTurningLeft;
 bool bTurningRight;

This is all the code to do with turning and moving at the moment.

avatar image Firefly74 Dec 05 '17 at 01:46 PM

so using

  FVector NewDuckLocation = GetActorLocation() + (TurningValue.RotateVector(Velocity) * DeltaTime);

doesn't work ? remove your binary folder and rebuild to be sure to have up to date binaries

avatar image MartyBro Dec 06 '17 at 11:05 AM

This works for moving along the y axis, or left and right, but when I attempt to move forward and backward along the x axis, I find that I can only move forward no matter which button I press, and if I try to go forward I end up going down and through the floor, and if I go backwards I end up going up and floating above the floor. I will try using clamp on the z axis of the duck, but I'm still not sure what to do about the only moving forwards thing.

avatar image MartyBro Dec 06 '17 at 11:08 AM

And by clamping the z axis, I now cannot move forward and backward at all

avatar image Firefly74 Dec 06 '17 at 11:23 AM

Then maybe try :

 FVector NewDuckLocation = GetActorLocation() + (FRotator(0, TurningValue.Yaw, 0).RotateVector(Velocity) * DeltaTime);
 
avatar image MartyBro Dec 06 '17 at 11:30 AM

I fixed it by changing the direction of movement from the x axis to the z axis, thank you for pointing me in the right direction

(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