Announcement

Collapse
No announcement yet.

Determine if player is accelerating

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

    Determine if player is accelerating

    Hi folks! I am having some issues determining if my character is accelerating using c++.

    I am trying to copy this behavior:
    Click image for larger version

Name:	wee.PNG
Views:	54
Size:	80.0 KB
ID:	1733504






    This makes it so when the player stops apply input, 'IsAccelerating" returns false immediately

    However, when I try and "GetCurrentAcceleration" this does not exist in C++, I tried to look at the code for the "GetCurrentAcceleration" node, however all it does is return acceleration, and shows no function how it is calculated.

    This is my current code, and it returns false only when the character has completely stopped.

    Code:
    fMovementSpeed = Pawn->GetVelocity().Size(); 
    
            //Is character moving?
            if (fMovementSpeed > 0)
            {
                bIsAccelerating = 1;
            }
            else
            {
                bIsAccelerating = 0;
            }
    Attached Files

    #2
    To determine acceleration you will need to remember the velocity of your character from the previous frame (the last time you got a tick). I think that's what the movement component is doing for you. In your own class you can achieve the same thing.

    Here's some quick code for you. I turned bIsAccelerating into a function.

    So, add a property to your class declaration (in your .h)

    Code:
    UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
    class YOUR_API UYourComponentNameHere: public UActorComponent
    {
        GENERATED_BODY()
    
        // .... some code
    
    protected:
        UPROPERTY()
        FVector _previousVelocity = FVector::ZeroVector;
    
        UPROPERTY(BlueprintReadOnly)
        FVector _currentAcceleration = FVector::ZeroVector;
    
    public:
        UFUNCTION(BlueprintCallable)
        bool isAccelerating()
        {
            return _currentAcceleration.Size() > 0.0f;
        }
    
        UFUNCTION(BlueprintCallable)
        FVector currentAcceleration()
        {
            return _currentAcceleration;
        }
    };
    Then in your TickComponent (in your .cpp):


    Code:
    // Called every frame
    void UYourComponentNameHere::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
    {
        Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
    
        // calculate the current acceleration
        FVector deltaV = GetOwner()->GetVelocity() - _previousVelocity;
        _currentAcceleration = deltaV / DeltaTime;
    
        _previousVelocity = GetOwner()->GetVelocity();
    
       // make sure you call your code after the above
    }
    Developing realtime biomolecular illustrative simulations in Unreal: Twitter - Youtube - GitHub

    Comment


      #3
      Thanks for the reply! Trying to understand why I cannot call the function though. After looking into the example Third Person C++ code, it almost seems as if I am getting the wrong component? In the example, the code for getting character movement is GetCharacterMovement(); however this is called from the Character class, not the anim class. Is there a way to call GetCharacterMovement from my anim class targeting the character?

      Comment


        #4
        In your custom class, which class are you inheriting from? In my example, UYourComponentNameHere is inheriting from UActorComponent.
        Developing realtime biomolecular illustrative simulations in Unreal: Twitter - Youtube - GitHub

        Comment


          #5
          Timtimmy, the class that I am trying to call this from inherits from AnimationInstance. I am trying to turn my AnimBP Event Graph into C++ code. In BPs, i have these values determined from the characters class, then casted to the character class from the animbp. I was trying to get these values from the character class within the animinstance class

          Comment


            #6
            I figured out what I was doing wrong the next day...

            I was trying to do this in an AnimInstance class, so I had to include UCharacterMovementComponent into my header file, then:

            Code:
              
                  APawn* Pawn = TryGetPawnOwner();
                 AYourCharacter* Character = Cast<AYourCharacter>(Pawn);
                 float Acceleration = Character->GetCharacterMovement()->GetCurrentAcceleration().Size();
            I know this does not give me the actual acceleration, rather, it gets the size, either 0, or 2048, if MaxAcceleration is set to 2048.

            But this is exactly what I needed. ) Leaving this here for others.
            Last edited by Darkloud1989; 03-19-2020, 11:00 PM.

            Comment

            Working...
            X