Game Development Community

Questions regarding sprint functionality.

by Korey Marciniak · in TGB Platformer Kit · 11/16/2008 (1:15 pm) · 3 replies

I have a few questions regarding adding character sprinting to the PSK. I have modified the gliding code to act in a similar way on the ground, however I have two issues currently and they are driving me insane.

Firstly is that when the character is using the sprint key his movement speed is increased, then it suddenly decides it wants to hesitate before continuing.

%this.MoveSpeed.X = mClamp(%this.MoveSpeed.X, -%this.MaxSprintSpeed, %this.MaxSprintSpeed);

%dir = -1 + 2 * (%this.MoveSpeed.X > 0);

From my best efforts, I feel these two lines are conflicting somehow. If the character runs without sprint, it will not drop the MoveSpeed.X at all, however if I am holding down the sprint key (currently bound to Z) he will run for quite some time and then randomly I will see MoveSpeed.X drop a bit (From 30 to around 18 give or take) before jumping back up to 30. I also see %dir become -1.

I don't see any other changes in variables while this is going on. The keys don't register as up, the velocity seems the same, and so on. The code is close enough to gliding that it makes me wonder what I could have messed up.



Secondly, is there an easy way to setup a cool down timer for this sprint feature? I've tried modifying this:

%this.SprintTime += getSceneTime() - %this.LastSprintTime;
		%this.LastSprintTime = getSceneTime();

However, it hurts my brain to try to understand how to count up to a timeout I set.


I am sure these are all stupid mistakes, but I need ideas from someone else, since I am giving myself a headache.


Thanks in advance! :)

About the author

Recent Threads


#1
11/20/2008 (12:46 pm)
Korey, just letting you know that I have seen this and I will try to find some time later today to answer!

Sorry for the delay.
#2
11/20/2008 (5:59 pm)
I don't really see a problem with what you've got there, maybe it is because of the way that you've implemented the actual sprinting?

Here is my quick and dirty implementation:

// Behavior Fields:
    %template.addBehaviorField( AllowSprint, "Allow the actor to sprint", BOOL, true);
    %template.addBehaviorField( MaxSprintSpeed, "Maximum sprinting speed", FLOAT, 50);
    %template.addBehaviorField( MaxSprintTime, "Maximum time sprinting", FLOAT, 1.00);
    %template.addBehaviorField( SprintTimeOut, "Minimum time between sprinting", FLOAT, 2.00);

function ActorBehavior::onAddToScene(%this, %scenegraph)
{
    ...
    %this.CanSprint = %this.AllowSprint;
    ...
}
 
function ActorBehavior::updateOnGroundPhysics(%this)
{
    ...
 
    // If the sprint button is released, stop sprinting
    if (%this.IsSprinting && !%this.Owner.Controller.Sprint)
        %this.IsSprinting = false;
    
    // Allow us to sprint again if we've exceeded the sprint timeout
    if (!%this.CanSprint && (getSceneTime() - %this.LastSprintTime) >= %this.SprintTimeOut)
        %this.CanSprint = true;
	
    // Update the sprint status
    if (%this.AllowSprint && %this.CanSprint && %this.Owner.Controller.Sprint)
    {
        if (!%this.IsSprinting)
            %this.LastSprintTime = getSceneTime();
      
        %this.SprintTime     += getSceneTime() - %this.LastSprintTime;
        %this.LastSprintTime  = getSceneTime();
		
        %this.IsSprinting = true;
    }
	
    // Check if we should continue to sprint
    if (%this.IsSprinting && %this.SprintTime >= %this.MaxSprintTime)
    {
        %this.SprintTime              = 0;
        %this.IsSprinting             = false;
        %this.CanSprint               = false;
        %this.Owner.Controller.Sprint = false;
        }
	
    // Make sure we dont move too quickly
    if (%this.IsSprinting)
        %this.MoveSpeed.X = mClamp(%this.MoveSpeed.X, -%this.MaxSprintSpeed, %this.MaxSprintSpeed);
    else
        %this.MoveSpeed.X = mClamp(%this.MoveSpeed.X, -%this.MaxMoveSpeed, %this.MaxMoveSpeed);
    
    ...
}

If you need any explanation, let me know.

Good luck!
#3
11/21/2008 (5:07 pm)
Thanks very much! :D

I tried implementing your version of it, and it took care of my cooldown issue, and it looks like my problem there was that I was trying to do it with too few variables and over-complicating the actual timer portion of it.

However, it still suffers from the same issue I noticed before. When sprinting the actor slows ever so slightly mid-sprint. It doesn't happen when running normally.

Here's what the MoveSpeed.X looks like when it happens while sprinting:

-30
-30
-15
-18
-21
-24
-27
-30
-30


(This is assuming MaxSprintSpeed is 30, and MaxMoveSpeed is 15)

So I am curious if it's possible that it is randomly slowing the character and returning to MaxMoveSpeed, before realizing the actor is sprinting and bumping it back up to MaxSprintSpeed?

Thanks again for the help, I appreciate the support :)