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"

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 https://www.youtube.com/watch?v=gkbCsIq_LXI). 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?

Product Version: UE 4.12
Tags:
more ▼

asked Aug 07 '16 at 02:03 AM in C++ Programming

avatar image

Boagz
50 14 21 28

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

1 answer: sort voted first

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.

more ▼

answered Aug 07 '16 at 02:34 AM

avatar image

DennyR
1.1k 31 12 47

avatar image Boagz Aug 07 '16 at 02:53 AM

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 = /

avatar image DennyR Aug 07 '16 at 03:26 AM

Oh I get it :D

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.

avatar image Boagz Aug 07 '16 at 03:40 AM

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

(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