Announcement

Collapse
No announcement yet.

Stamina System for UE 4.12

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

    Stamina System for UE 4.12

    To anyone who reads this, I am a beginner and I really didn't know what to do to get started so I started looking up tutorials. I came across one that was talking about adding a stamina system to a character, which I linked below, but it doesn't seem to work for my character/version of UE 4 (I have 4.12, newest one). I want my character to slow down after a while when he runs and gain stamina back as he runs, meanwhile, different things occur, like the stamina system not decreasing at all, or the stamina decreasing too much, going into the negatives (that's something else I experience too, I don't want the value to pass into the negative values). If anyone could maybe try to help me with this, or even come up with the whole stamina system (You don't have to if you don't want to, but it would really help if you did), it would be really appreciated.

    Here's that video I was talking about: https://www.youtube.com/watch?v=lfqYvPZdjsg

    #2
    Hi kingpanncake,

    Firstly, welcome to UE4

    I've been needing an excuse to make a tutorial video. I'll download 4.12 and make a tutorial on how to do this when I get home. I'm just starting a new project so good timing.

    I'll post it here when I am done.

    Thanks

    Parker
    Last edited by parker67; 06-09-2016, 02:50 AM.
    Twitter: https://twitter.com/favoritetortois
    Youtube: https://www.youtube.com/channel/UCyb...f-8pIldQ<br />

    Comment


      #3
      Thanks Parker!

      Comment


        #4
        This is my C++ implementation of a working stamina system, mostly inspired by Skyrim.

        I know this is C++ and you're looking for blueprints, but in my mind, they're both "programming" and how you end up constructing the system is less important than the system design and implementation. You should be able to piece together a lot of the working parts here and use that to inspire your own design.


        BaseCreature.h
        Code:
        //This is the current speed we would run at if we wanted to run as fast as we could.
        	//This can be affected by environmental effects such frost and tar.
        	UPROPERTY(BlueprintReadWrite, EditAnywhere, Transient, Category = "Creature|Movement")
        		float CurRunSpeed = MaxRunSpeed;
        
        	//This is the default walk speed of the creature. This shouldn't change very often.
        	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
        		float MaxWalkSpeed = 100;
        
        	//This is the current walking speed we'd have if we walked as fast as we could.
        	//This can be affected by environmental effects such frost and tar.
        	UPROPERTY(BlueprintReadWrite, EditAnywhere, Transient, Category = "Creature|Movement")
        		float CurWalkSpeed = MaxWalkSpeed;
        
        	/*This is how fast we're moving when we give movement input*/
        	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
        		float MoveSpeed = 100;
        
        	/*This is the most endurance we can have*/
        	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
        		float MaxEndurance = 10;
        
        	/*This is our current endurance. Some actions like sprinting uses this up.*/
        	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
        		float Endurance = 10;
        
        	/*Indicates whether or not the creature is burning endurance to move faster*/
        	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
        		bool bSprinting = false;
        
        	/*Indicates that the creature is exhausted. Exhausted creatures cannot perform actions which consume endurance.*/
        	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
        		bool bExhausted = false;
        
                UFUNCTION(BlueprintCallable, Category = "Creature|Movement")
        		void TrySprint();
        BaseCreature.cpp:
        Code:
        void ABaseCreature::TrySprint()
        {
        	if (!bSprinting && !bExhausted && Endurance > 0)
        	{
        		bSprinting = true;
        		MoveSpeed = MaxRunSpeed;
        	}
        }
        
        void ABaseCreature::Tick(float DeltaSeconds)
        {
        
        	Super::Tick(DeltaSeconds);
        
        	//If we're sprinting, we try to move at our run speed. If we get exhausted, we reduce our speed to our walk speed.
        	if (bSprinting)
        	{
        		
        		if (Endurance == 0 && !bExhausted)
        		{
        			bSprinting = false;
        			bExhausted = true;
        			MoveSpeed = CurWalkSpeed;
        			OnExhausted.Broadcast();
        			
        		}
        		else
        		{
        			Endurance -= DeltaSeconds;
        			Endurance = FMath::Clamp<float>(Endurance, 0, MaxEndurance);
        			MoveSpeed = CurRunSpeed;
        		}
        	}
        	else
        	{
        		//recover endurance
        		if (Endurance < MaxEndurance)
        		{
        			Endurance += DeltaSeconds;
        			Endurance = FMath::Clamp<float>(Endurance, 0, MaxEndurance);
        		}
        
        		//recover from exhausted state if we've recovered half our endurance
        		if (Endurance >= MaxEndurance / 2.f)
        		{
        			bExhausted = false;
        		}
        	}
        
        	
        	GetCharacterMovement()->MaxWalkSpeed = MoveSpeed;
        //other code removed...
        }

        Comment


          #5
          [MENTION=52471]parker67[/MENTION] Were you able to get the blueprints down?

          Comment


            #6
            yeah sorry. had some issues last night with getting UE 4.12. Ive got it now so ill do something tonight. Ill post it tonight (sorry in AEST time so its morning here )
            Twitter: https://twitter.com/favoritetortois
            Youtube: https://www.youtube.com/channel/UCyb...f-8pIldQ<br />

            Comment


              #7
              https://forums.unrealengine.com/show...aining-Stamina

              Comment


                #8
                Well that was fun, GG.Click image for larger version

Name:	UE4StaminaBluePrint4.12.2.png
Views:	1
Size:	644.4 KB
ID:	1108222
                Last edited by Tactical_Beard; 06-13-2016, 04:23 PM.

                Comment


                  #9
                  I love BP's but the C++ is just cleaner and performant.....

                  teak
                  "A little bit of nonsense now and then is cherished by the wisest men..."
                  -- Willy Wonka

                  Smooth Zoom Camera Plugin for 4.22 here.

                  Comment


                    #10
                    Your channel link is sorta messed up [MENTION=52471]parker67[/MENTION] I click on it and it says the channel isn't available

                    Comment


                      #11
                      Here is the link of his channel:

                      https://www.youtube.com/channel/UCyb...IzgY_Yf-8pIldQ

                      Seems that one of his html tags didn't convert properly. (I think he only needs to remove the whitespace in the tag).
                      Last edited by DarkGodsLair; 06-10-2016, 09:21 PM.
                      Main channel:
                      https://www.youtube.com/channel/UCGC...o9s3YcA/videos

                      Comment


                        #12
                        thanks [MENTION=172129]DarkGodsLair[/MENTION] Ill fix that now.

                        The video is currently uploading. Its a very quick video with no audio (due to all the background noise of living near a busy road). sorry bout the delay but this should help you.

                        video will be available when done at

                        https://youtu.be/jRlvliCDj4k

                        Will probably take a couple of hours due to my internet connection.
                        Twitter: https://twitter.com/favoritetortois
                        Youtube: https://www.youtube.com/channel/UCyb...f-8pIldQ<br />

                        Comment


                          #13
                          Thanks [MENTION=52471]parker67[/MENTION] I got it to work! Just one thing, for the - and + floats it was losing and gaining to slowly, so I raised it from 0.01 to 0.05. Does the trick! Thank you!

                          Comment


                            #14
                            Hi,
                            If you really want to make game, do this tutorial: http://shootertutorial.com/tutorials/
                            It is long and hard, but you will learn a lot...

                            Comment


                              #15
                              Tactical_Beard - How did you make that all one giant image? Was that photoshopped together?
                              And just one correction on those BPs. It's STAMINA ;D gave me a chuckle tho

                              Comment

                              Working...
                              X