I am trying to get my character to stop sprinting when aiming

Key configuration:
Sprinting = LeftShift
Aiming = Right Mouse Button

I suppose I should modify function ACharacter::SprintStart to add an:
if right mouse button is not pressed, MaxWalkSpeed=1000 else MaxWalkSpeed=500(remains unchanged)

Any insight would be very appreciated

A segment of the code

void ACharacter::SprintStart()
{
CharacterMovement->MaxWalkSpeed = 1000.f;
}

void ACharacter::SprintEnd()
{
CharacterMovement->MaxWalkSpeed = 500.f;
}

void ACharacter::AimStart()
{
bUseControllerRotationYaw = true;
}

void ACharacter::AimEnd()
{
bUseControllerRotationYaw = false;
}

Perhaps set the walkspeed in the Tick function and control it with a bool set in the SprintStart/End functions respectively.

Sorry, I’m a bit rusty on this. :stuck_out_tongue:

Hey, Madvoyer.

I’m a little rusty with C++, but couldn’t you just change your AimStart and AimEnd to look like this?

void ACharacter::AimStart() {

bUseControllerRotationYaw = true;

CharacterMovement->MaxWalkSpeed = 500.f;

}

void ACharacter::AimEnd() {

bUseControllerRotationYaw = false;

CharacterMovement->MaxWalkSpeed = 1000.f;

}

Edit: Actually, in hindsight, wouldn’t it be cleaner code to just call SprintEnd() in BeginAim()?

Regards,

Jonathan

Thank you for taking the time to answer my question.

Both of these technically work, but since I used the following earlier, the player can aim, release the shift key, press the shift key and move with the increased speed:

InputComponent->BindAction(“Sprint”, IE_Pressed, this, &ACharacter::SprintStart);
InputComponent->BindAction(“Sprint”, IE_Released, this, &ACharacter::SprintEnd);

InputComponent->BindAction("Aim", IE_Pressed, this, &A<Projectname>Character::AimStart);
InputComponent->BindAction("Aim", IE_Released, this, &A<Projectname>Character::AimEnd);

I would need to have a function that is always checking wether the shift key is pressed or not/speed of the character.

Ah, I see. What if you changed SprintStart() to this?

void ACharacter::SprintStart() {

if (bUseControllerRotationYaw==false)

CharacterMovement->MaxWalkSpeed = 1000.f;

}

That would prevent you from sprinting while aiming. It might feel better for the user, however, if you did it like this, letting them cancel aiming by sprinting:

void ACharacter::SprintStart() {

CharacterMovement->MaxWalkSpeed = 1000.f;

bUseControllerRotationYaw = false;

}

Thanks for the help, I arrived at something similar then adjusted with yours :slight_smile:

Glad I could help. :slight_smile: