Make Enemy move towards player

I’m trying to make my enemies run towards my player. For this I’m doing at every tick (I know I don’t need to get the player character every tick)

ACharacter* myCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);

FVector EnemyLocation = this->GetActorLocation();

FVector PlayerLocation = myCharacter->GetActorLocation();

FVector MovementVector = PlayerLocation - EnemyLocation;

AddMovementInput(MovementVector, 1.f);

I’ve checked and both Location Vector are outputting right, but for some reason my Enemy doesn’t move. What am I doing wrong?

Is your enemy a Pawn? From the docs: Base Pawn classes won’t automatically apply movement, it’s up to the user to do so in a Tick event. Subclasses such as Character and DefaultPawn automatically handle this input and move. Also, “MovementVector” should be normalized when you send it to AddMovementInput.

You can move your actor towards the Player by taking out your last line (AddMovementInput) and adding this:

FVector Vel = (MovementVector.Normalize() * YourPawnSpeed * DeltaTime);
SetActorLocation(EnemyLocation + Vel);

Thanks! Just one thing. I think you should do the Normalize before and then do what you told, as Normalize returns a bool.

It works!

Hi I’m looking to design my own side-scrolling shooter based on your content that i purchased, But when I attempt to import my own meshes, because the current mesh is inherited i can’t position it to face my player on-screen during the game, my own imported ships either come out lop-sided or shoot the wrong way,I would be grateful for any help or advice you can give me on this, All in all, the content is pretty awesome and i feel it will help me take my first steps into game development.

Kind Regards,

.

I’ve tried this line of code in my own game and I always end up with a ‘no suitable constructor to convert from to’ error. Do you know how I would go about fixing this?