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"

Rotating Character Towards Mouse In C++, Buggy Code

Hey,

So I'm trying to make my player rotate towards the mouse. I've created a method to do this, but it causes the player to rapidly rotate in many directions, and I'm not sure why.

Here's the method:

 void ATopDownPlayerController::RotatePlayerToMouse()
 {
     //Player exists.
     if (ATopDownCharacter* TopDownCharacter = Cast<ATopDownCharacter>(GetCharacter()))
     {
         //Get mouse rotation.
         FVector MouseLocation, MouseDirection;
         this->DeprojectMousePositionToWorld(MouseLocation, MouseDirection);
 
         //Get the rotation needed for the player to face the mouse.
         FRotator TargetRotation = UKismetMathLibrary::FindLookAtRotation(TopDownCharacter->GetActorLocation(), MouseLocation);
 
         //Set new yaw rotation while retaining current pitch and roll.
         ControlRotation = FRotator(GetControlRotation().Pitch, TargetRotation.Yaw, GetControlRotation().Roll);
     }
     else
     {
         UE_LOG(LogTemp, Error, TEXT("Pointer to character is null!"));
     }
 }


Am I doing this correctly? What would be the proper way to accomplish this in C++?

alt text

Product Version: UE 4.16
Tags:
more ▼

asked Jul 18 '17 at 07:35 PM in C++ Programming

avatar image

Ovicior
12 2 4 6

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

1 answer: sort voted first

Found a solution. I use a custom trace collision channel and invisible plane to determine rotation.

Here's the code for the controller:

 void ATopDownPlayerController::RotatePlayerToMouse()
 {
     //Player exists.
     if (ATopDownCharacter* TopDownCharacter = Cast<ATopDownCharacter>(GetCharacter()))
     {
         //Store data returned by UE.
         FVector MouseLocation, MouseDirection, MouseLocationEnd;
         FHitResult HitResult;
 
         //Use player controller and get mouse info.
         this->DeprojectMousePositionToWorld(MouseLocation, MouseDirection);
 
         //Make trace long so it hits anything nearby.
         MouseLocationEnd = (MouseDirection * 10000) + MouseLocation;
 
         //Stores settings for raycast.
         FCollisionQueryParams TraceSettings;
         FCollisionResponseParams TraceResponses;
 
         //Do trace. If it succeeds, complete calculations and set new rotation.
         if (GetWorld()->LineTraceSingleByChannel(HitResult, MouseLocation, MouseLocationEnd, MouseRotation, TraceSettings, 
                                                  TraceResponses) == true)
         {
             //Calculate new rotation with actor's location and hit result.
             FRotator NewRotation = UKismetMathLibrary::FindLookAtRotation(TopDownCharacter->GetActorLocation(), HitResult.ImpactPoint);
             TopDownCharacter->SetActorRotation(FRotator(GetControlRotation().Pitch, NewRotation.Yaw, GetControlRotation().Roll));
         }
     }
     else
     {
         UE_LOG(LogTemp, Error, TEXT("Pointer to character is null!"));
     }
 }


I also use a blueprint to set the trace channel on the plane.

more ▼

answered Jul 19 '17 at 05:21 AM

avatar image

Ovicior
12 2 4 6

avatar image koise Dec 08 '18 at 09:36 PM

So where is the MouseRotation coming from? If you are referencing the MouseDirection previously declared it's saying that you cannot cast a FVector/FRotator into a ECollisionChannel

(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