Announcement

Collapse
No announcement yet.

Character Movement Direction.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Character Movement Direction.

    Hello am setting up some new animation Blueprints.
    And having some trouble getting the direction of my character.

    Now speed was not a problem.
    Code:
    Character->GetVelocity().Size();
    But can someone give me some advice on direction?

    How can i determine if my character is moving left or right.
    Backwards or forward, or at a angel?

    To illustrate i made this.
    Click image for larger version

Name:	hXXs4WM.png
Views:	1
Size:	8.2 KB
ID:	1150407

    Thanks for any advice.
    WCode

    #2
    Forward/backwards/left/right are all relative, so generally what you do is check the input values themselves. If I get the input values for my "MoveForward" axis, then I know > 0.0 is moving forward, < 0.0 is moving backwards. Same with "MoveRight" to determine left/right movement.

    Here's some helpful links on this:
    https://docs.unrealengine.com/latest...put/index.html
    https://docs.unrealengine.com/latest...ace/index.html

    Comment


      #3
      Hey thanks alot mate!
      It never struck my mind to get the axis value.

      For anyone intrested here is the method, I use in my Anim Graph.
      Code:
      FVector UMyAnimationInstance::GetPlayerMovementInput()
      {
      	AMyPlayerClass* MyPlayer = Cast<AMyPlayerClass>(GetOwningActor());
      	if ((MyPlayer) && MyPlayer->InputComponent)
      	{
      		return FVector(
      				MyPlayer->InputComponent->GetAxisValue(TEXT("MoveForwardBackward"))
      				, MyPlayer->InputComponent->GetAxisValue(TEXT("MoveRightLeft"))
      				, 0);
      	}
      	else
      		return FVector(ForceInit);
      }
      Cheers!
      Last edited by WCode; 05-21-2015, 05:28 PM.

      Comment


        #4
        For most cases using a standard character, there will not be a big difference between axis values and movement speed. Axis values are translated more to acceleration, but generally for characters there is not much drifting going on so using input values will probably work fine.

        But if you really want to know instantaneous movement direction, you can use the dot product as follows. This will deal correctly with cases where, for example, your input is reverse, but your characters momentum is still carrying him forwards.

        Code:
        FVector Vel = Character->GetVelocity();
        FVector Forward = Character->GetActorForwardVector();
        FVector Right = Character->GetActorRightVector();
        float ForwardSpeed = FVector::Dot(Vel, Forward);
        float RightSpeed = FVector::Dot(Vel, Right);
        To clarify, these values give you the components of velocity in the direction the character is facing, and perpendicular to the direction the character is facing. Negative values mean the character is moving backwards/to his left.

        Comment


          #5
          Originally posted by kamrann View Post
          For most cases using a standard character, there will not be a big difference between axis values and movement speed. Axis values are translated more to acceleration, but generally for characters there is not much drifting going on so using input values will probably work fine.

          But if you really want to know instantaneous movement direction, you can use the dot product as follows. This will deal correctly with cases where, for example, your input is reverse, but your characters momentum is still carrying him forwards.

          Code:
          FVector Vel = Character->GetVelocity();
          FVector Forward = Character->GetActorForwardVector();
          FVector Right = Character->GetActorRightVector();
          float ForwardSpeed = FVector::Dot(Vel, Forward);
          float RightSpeed = FVector::Dot(Vel, Right);
          To clarify, these values give you the components of velocity in the direction the character is facing, and perpendicular to the direction the character is facing. Negative values mean the character is moving backwards/to his left.
          This is a good point, in all honestly you'll probably want both methods: Input Direction, and Velocity Direction so you can better tailor your blend spaces to match what is happening to the characters vs player intent.

          Comment


            #6
            Ahe yes this was defenetly a better solution.
            Thank you so much for your help guys!

            Cheers!

            Comment

            Working...
            X