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"

[C++] Disabling strafe and backwards movement while sprinting

Nothing special about my code, but im trying to find a way to make it so that my character can only run forward as he sprints. I've tried putting the code in my MoveRight() function in an if statement that is called when isSprinting is false but that just makes it an error. Below is my code for my sprint function, what should i add to only allow forward movement?

    void ABaseHero::StartSprint()
     {
         GetCharacterMovement()->MaxWalkSpeed = RunSpeed;
         isSprinting = true;
     }
     
     void ABaseHero::StopSprint()
     {
         GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
         isSprinting = false;
     }
Product Version: UE 4.16
Tags:
more ▼

asked Sep 22 '17 at 11:51 PM in C++ Programming

avatar image

Overeptic
7 4 6 8

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

1 answer: sort voted first

I figured out how to do it. My sprint functions are below

 void ABaseHero::StartSprint()
 {
     GetCharacterMovement()->MaxWalkSpeed = RunSpeed;
     isSprinting = true;
 }
 
 void ABaseHero::StopSprint()
 {
     GetCharacterMovement()->MaxWalkSpeed = WalkSpeed;
     isSprinting = false;
 }


And here is my MoveForward and MoveRight functions

 void ABaseHero::MoveForward(float Value)
 {    
     if (isSprinting && Value < 0)
     {
         AddMovementInput(GetActorForwardVector(), Value * 0.5);
     }
     else
     {
         AddMovementInput(GetActorForwardVector(), Value);
     }
 }
 
 void ABaseHero::MoveRight(float Value)
 {
     if (isSprinting)
     {
         AddMovementInput(GetActorRightVector(), Value * 0.5);
     }
     else
     {
         AddMovementInput(GetActorRightVector(), Value);
     }
 }

TO BE CLEAR. I know I shouldn't have hard coded the 0.5 in there, but that is just my temporary solution. I will most likely replace that with WalkSpeed ÷ RunSpeed.

more ▼

answered Sep 23 '17 at 08:23 PM

avatar image

Overeptic
7 4 6 8

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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