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"

Why isn't VInterpTo(AActor->GetForwardVector()) moving forward?

I'm trying to write a function that will VInterpTo() every tick to move the player an arbitrary distance in a given direction, in this case in the direction he's facing:

 void UMovementNotifyState::NotifyBegin(USkeletalMeshComponent * MeshComp, UAnimSequenceBase * Animation, float TotalDuration){
     myActor = Cast<AActor>(MeshComp->GetOwner()); //Cache a reference to the ACharacter to be moved, as an actor
     myCharacter = Cast<ACharacter>(MeshComp->GetOwner());
     if (myCharacter != nullptr && myActor != nullptr){ //Sanity check to prevent Persona crashing
         startLocation = myCharacter->GetActorLocation(); //Set the starting node to our current position
         endLocation = myCharacter->GetActorForwardVector()*moveDistance; //Set the ending node to a position float moveDistance unreal units forward of our current facing
     }
 }
 
 void UMovementNotifyState::NotifyTick(USkeletalMeshComponent * MeshComp, UAnimSequenceBase * Animation, float FrameDeltaTime){
     if (myCharacter != nullptr && myActor != nullptr){//Sanity check        
                 float Alpha = 5;                
                 destinationLocation = FMath::VInterpTo(startLocation, endLocation, FrameDeltaTime, Alpha);
                 myCharacter->SetActorLocation(destinationLocation,true);
     }
 }

This compiles fine, but the movement it creates is seemingly random: movement direction is independent of the character's facing, and seems to either push him away in a random direction, or suck him towards the world origin. The most obvious explanation in my mind is that the vector math is wrong, and endLocation doesn't point to a position n units in front of the character like I think it does, but I'm really lost when it comes to how to fix it.

Product Version: UE 4.8
Tags:
more ▼

asked Jul 14 '15 at 03:07 PM in C++ Programming

avatar image

HInoue
251 97 67 83

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

1 answer: sort voted first

I am guessing that you need to add startLocation to your current endLocation, as your endLocation will always be around 0 since GetActorForwardVector is a unit vector pointing forward, not a position in space in front of your character. He's probably always moving to a location kind of by (0,0,0).

Edit: Like this.

alt text

position.png (6.1 kB)
more ▼

answered Jul 14 '15 at 03:12 PM

avatar image

mrooney
703 40 32 123

avatar image HInoue Jul 14 '15 at 04:27 PM

Hmm, I kind of see what you mean, I was definitely stuck thinking of the vector as a coordinate triple, not a unit vector. You mean add the two together when I first calculate endLocation like this, not inside the final interpolation, right?

 endLocation = (myCharacter->GetActorForwardVector() + startLocation) *moveDistance;

That's the only thing I changed from the original post, but he still seems to get sucked towards the origin. I made a handy-dandy video showing this in action- the notify is on his jump animation, so every time it jumps the vinterpto logic runs: http://imgur.com/YCdxqmD

Since he's still getting sucked into the same rough area, I'm convinced trying to grab the forward vector is the culprit. I tried a slightly different approach, and added rotation in while I was at it:

     FVector movementVector = myActor->GetActorForwardVector()*moveDistance; //Set the ending node to a position float moveDistance unreal units forward of our current facing
     movementVector = movementVector.RotateAngleAxis(moveDirection, FVector(0, 0, 1));
     endLocation = movementVector;

The rotation part works fine, but it still ends up getting sucked into the same corner whenever it fires.

avatar image mrooney Jul 14 '15 at 05:10 PM

I was definitely stuck thinking of the vector as a coordinate triple, not a unit vector.

It is kind of both. The problem you're having is that the forward vector is just a directional vector. It isn't a point in space in front of the player. It's the direction the player is facing from the origin. It's a coordinate triple relative to the player's location, not relative to the world.

You mean add the two together when I first calculate endLocation like this, not inside the final interpolation, right?

Like this:

 endLocation = startLocation + (myCharacter->GetActorForwardVector() *moveDistance);

Your new one will move to a really odd location depending on where the current start location is. For example if you are at (1,1,1) facing down the X axis with forward vector (1, 0, 0) and you want to move 2 units that way, you want to end up at (3,1,1) not (4,2,2).

avatar image HInoue Jul 14 '15 at 11:57 PM

The sound you don't hear is a boatload of celebratory cymbals and fireworks going off, because this finally works- thank you so much for your patient explanation and the illustration! Vector math scares the heck out of me every time I try to make it work :)

(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