Make player character lock on and move toward target

I’m creating a 2d fighing game. In it, the fighters can fly and battle. Currently, characters are just flying around free form. My issue is I need a way to have the player ‘lock on’ mid battle if they want to and have them face and then move to wherever the opponent goes (similar to how it was setup in the dragonball z budokai series DBZ Budokai 3 Online - Goku [Mourossonero] vs Cell [Tico] 8 Fights - YouTube). So far I’ve been able to orient the player character’s rotation to always face the oppoent but I do not know how to have them follow wherever the opponent goes. Any suggestions?

I am not sure if I understand everything you are asking, but I think you want the movement to locked to the target, right? So if the target moves, the distance should stay the same?

If so, then you can do this in two ways.

First implementation
Read the location of your target every frame and store it’s location. Then just calculate the difference in location and add it to your own location. You would have to make sure that you aren’t colliding with anything though.

Second implementation
Store the Vector Target->GetActorLocation() - this->GetActorLocation() then every frame, calculate a new position based on this vector (from the target to the new point) and smootly interpolate towards this new location.

Ya sorry. Sometimes it hard to illustrate my problem. Currently, I have basic movement input setup on my character. So the character will fly in the direction the user inputs. So if the player moves say the thumb stick on an xbox controller to the right, the character will move right (makes sense). What I want to happen when the player ‘locks on’ to an opponent is have the right or left movement of the thumb stick move the player in the direction of the opponent. So if the opponent is currently 10 units to the right, or in the positive x direction, and 100 units above, or in the positive y direction, then pressing right on the thumb stick on a controller will cause the player character to move in a positive, diagonal line towards the opponent. This line towards the opponent I guess would be updated every frame. So in a way I guess I’m asking how I can manipulate a player characters movement in game so that the player moves in a slightly modified direction from the direction input. Not sure if that explanation helped any = /

Oh I get it :smiley:

Yeah, you would have to check if the stick value is left or right. If it is right (Input>0) then move towards the target.

You can get the direction vector by subtstracting both locations (like I posted in the first answer)

FVector DeltaLocation = Target->GetActorLocation() - this->GetActorLocation();

So now when the stick input is to the right side, just move your player in this direction. You would need to specify a speed variable or something and then move your player along this Direction ever frame. Code could look like this:

FVector DeltaLocation= Target->GetActorLocation() - this->GetActorLocation();
FVector DesiredNewLocation = this->GetActorLocation() + DeltaLocation;

FVector NewFrameLocation = FMath::VInterpTo(this->GetActorLocation(), DesiredNewLocation, DeltaTime, InterpSpeed);

this->SetWorldLocation(NewFrameLocation );

Call this every frame and your player should move forward. InterpSpeed is a variable that you have to set.

FMath::VInterpTo() smoothly interpolates between the current location and the given desired location.

EDIT: I just typed this up, there might be typos in the code, let me know if it works.

Okay great, thanks. I’ll give this a shot