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.
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).
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?
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: Imgur: The magic of the Internet
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.
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?
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).
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