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"

AnswerHub Maintenance

Background maintenance is scheduled to occur between 9 - 11am EDT on Tuesday, May 21. Site operation may be slower than normal during this time and a brief interruption in operation may be observed

how to use "MovetoLocation" to move character

please excuse my stupidness if i am doing something simple wrong but i am new to developing with unreal engine. What i am trying to do is move my character from start of maze to end of maze and have accomplished it using blueprints but i want to implement it in c++.

I Have assigned my characters AI Controller Class to my AIController.

in my AiController class i have

 void AMyAIController::MoveEnemyToTarget(){
     AMyEnemyCharacter* const Enemy = Cast<AMyEnemyCharacter>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));
     if (Enemy != NULL){
         FVector location = FVector(1962, 1270, 260);
         AMyAIController::MoveToLocation(location, 10.0f, true, true, true);

and then in my Characters BeginPlay() i have

     AMyAIController* ai = Cast<AMyAIController>(AMyAIController::StaticClass());
     FVector location = FVector(1962, 1270, 260);
     if (ai != NULL){

When i go to play it just dose nothing.. any help greatly appreciated as it has been annoying and hold me up all day

Product Version: UE 4.9
more ▼

asked Nov 25 '15 at 06:14 PM in C++ Programming

avatar image

1 2 5 6

(comments are locked)
10|2000 characters needed characters left

3 answers: sort voted first

Hi Dobby,

The AI Controller should automatically possess the character when it spawns so you should be able to remove that bit. To get a reference to the controller you should call GetController instead of StaticClass.

Try replacing your BeginPlay function with this

 void AMyCharacter::BeginPlay()
     if( GetWorld() && !GetWorld()->bStartup && GetController() == nullptr )
     AMyAIController* ai = Cast<AMyAIController>(GetController());
     FVector location = FVector(1962, 1270, 260);
     if( ai != NULL ) {

In your character constructor, make sure to set the default controller class (you do use StaticClass here):

 AIControllerClass = AMyAIController::StaticClass();
more ▼

answered Nov 25 '15 at 06:31 PM

avatar image

866 36 14 59

avatar image Dobby90 Nov 25 '15 at 06:53 PM

Still wont work for me :( would the fact that i have the player controller class to for controlling my camera and other game objects make a difference??

avatar image Alderbit Nov 25 '15 at 07:04 PM

I don't think that would cause any problems.

Try setting a breakpoint in BeginPlay on the line where you cast to your AI Controller class. See if the variable ai is null or if MoveToLocation is failing.

avatar image Dobby90 Nov 25 '15 at 07:28 PM

it keeps coming back from the cast as null

avatar image Dobby90 Nov 25 '15 at 10:26 PM

Thanks Alderbit. this worked for me eventually when i created a new AI controller class and new character class.. not sure what the initial problem was

(comments are locked)
10|2000 characters needed characters left

Well, if it doesn't cast to AIController then it's either null or a PlayerController. This basically means whole your setup and/or assumptions are wrong and I suggest grabbing any on UE4 samples that have AI in it and see how it's done there.

There's a lot of places where stuff could have gone bad for you, so it's a wast of time trying to solve it via an answerhub thread. Just look at samples.



more ▼

answered Nov 25 '15 at 07:33 PM

avatar image

MieszkoZ STAFF
7.3k 223 57 412

(comments are locked)
10|2000 characters needed characters left

Take a look at the UE4 sample project TopDown in C++. In that project the character moves where you click. This is done using the function MoveToLocation(...). It is very simple and well documented.

Hope this helps!

more ▼

answered Nov 28 '15 at 03:54 PM

avatar image

338 16 24 27

(comments are locked)
10|2000 characters needed characters left
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