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"

Character not rotating towards mouse when moving.

Hey, i'm working on a small project where the player controls a character in a top down view. The player can use wasd to move the character and use the mouse to make the character rotate. I'm currently using the third person template although i have rewritten some things.

I have managed to get so the character can rotate towards the mouse, but this only works if the character is standing still. The reason i'm using planes and line intersection is because i need to have the character able to rotate towards the mouse when the character is standing on something that is tilted.

 // Create plane with actors up vector and players location
 FVector ActorUp = GetActorUpVector();
 FPlane RotationPlane = FPlane(GetActorLocation(), ActorUp);
 
 //DrawDebugSolidPlane(GetWorld(), RotationPlane, GetActorLocation(), 250.0f, FColor::Red);
 
 // Get mouse location in the world
 FVector MouseDir;
 FVector MouseWorldPosition;
 PlayerController->DeprojectMousePositionToWorld(MouseWorldPosition, MouseDir);
 
 // Create a point under the mouse world position to use when creating the line
 FVector MouseWorldPosDown;
 MouseWorldPosDown = MouseWorldPosition - (ActorUp * 100.0f);
 
 // Intersection point between the line and plane
 FVector IntersectPoint;
 IntersectPoint = FMath::LinePlaneIntersection(MouseWorldPosition, MouseWorldPosDown, RotationPlane);
 
 // Find the direction to the projected point from the player
 FVector IntersectDirection = IntersectPoint - GetActorLocation();
 IntersectDirection.Normalize();
 
 // Draw line in the direction to look
 FVector Temp = GetActorLocation() + (IntersectDirection * 1000.0f);
 DrawDebugLine(GetWorld(), GetActorLocation(), Temp, FColor::Magenta);
 
 // Set the actor rotation
 SetActorRotation(IntersectDirection.Rotation());

This works fine when the character is standing still but if i start moving the character it stops working and start to look in weird directions. Since i'm using the third person template with the animation starter pack might this cause issues for the rotation?

However if i use this piece of code instead of the other set rotation it seems to work just fine. Why is this?

 SetActorRotation(FRotator(0.0f, MouseDir.Rotation().Yaw, 0.0f));
Product Version: Not Selected
Tags:
more ▼

asked Apr 05 '15 at 11:35 AM in C++ Programming

avatar image

Solicio
200 54 53 64

avatar image Solicio Apr 05 '15 at 12:08 PM

Continued testing a bit more, turned off the animations and it still behaves in the same way. Is there a way to stop the character so that it wont rotate in the direction the character is moving perhaps?

avatar image gormed Apr 05 '15 at 12:15 PM

In the character movement component, search for the Orient to controller or movement (dont have the engine or code at hand), there are two sources that influence the behavior. Look for the docs and other questions here in the hub:

https://answers.unrealengine.com/questions/15538/character-movement-orientation.html

avatar image Solicio Apr 05 '15 at 12:29 PM

I found what you were talking about, i turned off "Orient rotation to Movement" which stopped the character from rotating in the direction it is moving but i still had the issue that it was not rotating towards the mouse.

avatar image Solicio Apr 05 '15 at 12:59 PM

This is a screen shot of what is happening. Currently in the screen shot the player is moving to the west and the mouse is currently (although you can't see it) to the north west-ish. As you can see both the pink line which indicates which way the character should rotate and the character which is rotated to the pink line is in the wrong direction. But it is not always in the opposite direction.

alt text

So it seems something goes wrong in the code that calculates the direction when the character starts to move.

avatar image gormed Apr 05 '15 at 01:31 PM

Well if you want to move through the mouse, i would do the following: 1. use orient to movement. 2. project the mouse position to the plane you are moving on 3. then set the movement vector to move towards the projected position (do not rotate manually)

avatar image Solicio Apr 05 '15 at 01:38 PM

I could do that but i don't think it would fit the game that i'm creating.

avatar image gormed Apr 05 '15 at 01:51 PM

if you want to rotate faster use the rotation rate. as far as i know this is the recommeded way to rotate a pawn. also, you may reuse that for AI movement.

avatar image Solicio Apr 05 '15 at 02:00 PM

When using rotation rate i don't set the rotation directly but instead have the pawn rotate to a certain point over time, right? Even if i switched to this, i would still need to get the correct direction to the mouse when the player is moving. Which is currently not working.

avatar image gormed Apr 06 '15 at 04:48 PM

You have to use AddMovementInput(Direction, Value); where the direction is the vector form your char to the deprojected mouse position. Value is in you case most likely 1.0f. See the documentation for more on this.

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

0 answers: sort voted first
Be the first one to answer this question
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