Announcement

Collapse
No announcement yet.

Face movement input direction?

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

    Face movement input direction?

    Hello! For my project, I have a spring arm camera, attached to the capsule component of my pawn, and just add input movement input in the direction of the camera.
    However, I want my character to only run "forwards" as in, instead of running backwards, strafing left/right, etc. I want my character to always face the direction it's running in. Rather always facing the camera direction.

    Do I just rotate the mesh?
    Now we're getting into areas I'm really fuzzy at, getting a hold of variables, casting them the right way, etc.
    I haven't quite figured out how to get a hold of my blueprint's skeletal mesh in C++, how to reference it, etc.

    #2
    Alright, so I tried enabling "Orient to Movement", but it doesn't seem to work very well. It angles and offsets the camera slightly, but the skeletal mesh still faces the same direction.
    This is currently how I handle the strafing movement:
    Code:
    void ATribesmenCharacter::Strafe(float Value)
    {
        if ((Controller != NULL) && (Value != 0.0f))
        {
            //Find direction
            const FRotator Rotation = Controller->GetControlRotation();
            const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
            AddMovementInput(Direction, Value);
        }
    }
    Any suggestions?
    Last edited by Graylord; 02-15-2020, 07:30 AM.

    Comment


      #3
      alrighty

      to get your skeletal mesh from a character class use GetMesh()

      example:

      Code:
      USkeletalMeshComponent* SM = GetMesh();
      if (SM)
      {
          do something;
      }

      Suggestion for facing the way you're moving, this will have the effect that the player is now disconnected from the camera rotation though you'll have to code the camera rotation separately, i use this in a isometric view so may not be 100% what you need but should get you started:

      You can whack all this in the constructor of your character class:


      For the spring arm component of the camera (in my code i've called it SpringComp obviously, you call it what you want or do these settings in blueprint instead):

      Code:
          SpringComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringComp"));
          SpringComp->SetupAttachment(RootComponent);
          SpringComp->bUsePawnControlRotation = false;
          SpringComp->bInheritPitch = false;
          SpringComp->bInheritYaw = false;
          SpringComp->bInheritRoll = false;
          SpringComp->TargetArmLength = 1000.0f;
          SpringComp->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f)); //you probably won't need this bit unless you want an isometric view ;)


      Character movement settings:

      Code:
          UCharacterMovementComponent* CM = GetCharacterMovement();
          if (CM)
          {
              CM->RotationRate = FRotator(0.0f, 0.0f, 1000.0f); // I've set this really high so the rotation is pretty much instant - play with this to see what value works best for you
              CM->bOrientRotationToMovement = true;
              CM->bUseControllerDesiredRotation = true;
          }



      These are just character class settings:

      Code:
          bUseControllerRotationYaw = false;
          bUseControllerRotationPitch = false;
          bUseControllerRotationRoll = false;



      Then if you want to swap to change the character to always face a certain direction whilst moving just change this:

      Code:
          GetCharacterMovement()->bOrientRotationToMovement = false;

      And you'll have to update the control rotation probably each tick aswell for example:

      Code:
          AController* PC = GetController();
      
          if (PC)
          {            
                  PC->SetControlRotation(DirectionToTarget);
          }

      Using this method I believe it automatically uses the replication system aswell so works nice in multiplayer...well does for me i'm sure there's probably better ways

      happy valentines day


      EDIT:

      Just thought not sure if your movement code would work with that setup so here's my amended movement code from the character default, it just uses the camera rotation instead:

      Code:
      void AGCharacter::MoveForward(float Value)
      {
      
          if (CameraComp)
          {
              FRotator CamDirection = CameraComp->GetComponentRotation();
              FRotator CamYaw = FRotator(0.0f, CamDirection.Yaw, 0.0f);
      
              FVector MoveDir = CamYaw.Vector();
      
              AddMovementInput(MoveDir * Value);
          }    
      }
      
      void AGCharacter::MoveRight(float Value)
      {
          if (CameraComp)
          {
              FRotator CamDirection = CameraComp->GetComponentRotation();
              FRotator CamYaw = FRotator(0.0f, CamDirection.Yaw, 0.0f);
      
              FVector MoveDir = FRotationMatrix(CamYaw).GetScaledAxis(EAxis::Y);
      
              AddMovementInput(MoveDir * Value);
          }        
      }
      Last edited by savagebeasty; 02-14-2020, 08:29 PM.

      Comment


        #4
        Thanks! I already have my own springarmcomponent set up, so I think I should be good to go with the examples you given me!

        Hopefully this should work, but I'm a bit concerned that my character always faces the controller direction, even if while it stands still and I have at no point made the character lock to the controller rotation. (Maybe it's some default setting in the blueprint I've missed?) and that these setups are going to fight eachother, which would also explain why orentiationtomovement setting doesn't work.
        Last edited by Graylord; 02-15-2020, 07:58 AM.

        Comment


          #5
          Well that should sort you out then i think as these settings are stopping your character from inheriting the controller rotations:


          bUseControllerRotationYaw = false;
          bUseControllerRotationPitch = false;
          bUseControllerRotationRoll = false;

          if you leave orient to movement as false with these other settings your character should not rotate at all. Setting that to true will rotate the character to the movement direction.

          The movement code i put up will make it so movement is always relative to the camera yaw rotation rather than world space aswell. It should be like world of warcraft style movement in a 3rd person view.

          in relation to your other question about stafing. Take a look at the animation starter pack and have a look a look at the blendspace and anim blueprint it uses. That will take care of that type of rotation and movement.

          Personally i have two different locomotion states in my anim blueprint. One for facing movement direction and another for when you want to lock the rotation to a certain direction i.e. locked onto an enemy.

          If you get stuck give me a shout i can show you some screenshots of my setup.

          Comment


            #6
            sorry not sure if you still care but I noticed there was an error in the code I gave you......

            RotationRate = FRotator(0.0f, 0.0f, 1000.0f); is totally wrong, I'd not noticed I'd overridden that in the blueprints. it should have been: RotationRate = FRotator(0.f, 1000.0f, 0.f); No idea why that displays the params in a different order in blueprint apologies if that screwed you up at all

            Comment

            Working...
            X