Game Development Community

Enabling your players to walk and run

by Timlon · in Torque Game Engine · 12/09/2005 (1:37 am) · 28 replies

Hi,

I have changed some of the settings in server/scripts/player.cs to:

maxForwardSpeed = 4; //14;
maxBackwardSpeed = 3; //13;
maxSideSpeed = 3; //13;

maxUnderwaterForwardSpeed = 2.5; //8.4;
maxUnderwaterBackwardSpeed = 2; //7.8;
maxUnderwaterSideSpeed = 2; //7.8;

to allow my player character to (kinda of) walk instead of run. Ideally what I would like is to be able to have the player hold down say shift to make the character run - I have looked and looked but can't see a way to do it....can anyone help/advise?

Thank you :)

Timlon
Page «Previous 1 2
#1
12/09/2005 (2:20 am)
There's a modify max speed function, but I think that was just in the Realm Wars demo... oh wait, you have the SDK. Well without modifying source code there's no good way to do that. There is a resource that can give you a general idea on how to modify player speed, though I wouldn't necessarily copy it verbatim. Basically you'll need have a variable that gets updated on server and client that you can modify from script to change your speed.

http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=8463
#2
12/09/2005 (4:40 am)
There *is* a way to do it without any code changes.

You need to load two copies of your player model, each one assembled by a different CS file. In one of them, use a running animation as "run", and in the other, use a walking animation instead. Then setup two player datablocks with different maxForwardSpeed values (one for running and one for walking). Now you can use setDataBlock() to switch the player's datablock between the walking one and the running one.

The only major issue is the lack of interpolation between running and walking.
#3
12/09/2005 (5:47 am)
Thanks guys - much appreciated :) I will try both your suggestions :)
#4
12/09/2005 (6:44 am)
Umm Im really not sure, but couldnt you just set the player speed to a variable that is set too the players tun speed when the button is held down, and then the walking speed when its not? Then for running just speed up the animations?
#5
12/09/2005 (7:26 am)
The run animation will look like the character is running in slow motion... at a walking speed but still slow motion. Better to have a walking animation to call on I think.
#6
12/09/2005 (7:27 am)
Ben the player speed is defined in the player datablock and you can't use a variable as a value for a datablock, it has to be a specific value.

in my experience, player movement in torque can be accomplished in two different ways. you can use the higher-level $mvforward, $mvbackward etc. globals to activate player movement described by the datablock and model animations, or you can take the external approach using setvelocity() to move and playthread() to call the right animations explicitly. taking the first approach, Manoel's solution would probably be best. use multiple datablocks with different run animations and max speeds and swap them in-game. using the second approach, Ben's solution would be feasible and the animations could be called on the fly.
#7
12/09/2005 (7:35 am)
Ok, :)
#8
12/09/2005 (7:39 am)
I had a quick glimpse at this post and thought i'd challange myself to see if i could come up with a quick dirty solution. Well it took me 2 minutes to think up & implement and it totally sucks but i'm gonna post it anyhow cos it's so dodgy it's funny it actually works :)

-In starter.fps/client/scripts/ create 2 new *.cs scripts. Name one run.cs, the other walk.cs

-Inside run.cs place the following...
datablock PlayerData(PlayerBody : Run)
{
maxForwardSpeed = 50;
};

-Inside walk.cs place the following...
datablock PlayerData(PlayerBody : Walk)
{
maxForwardSpeed = 14;
};

-In starter.fps/client/scripts/default.bind.cs add the following...
function run()
{
   exec("./run.cs");
}

function walk()
{
   exec("./walk.cs");
}

moveMap.bind( keyboard, rshift, run );
moveMap.bind( keyboard, lshift, walk );

-Delete your dso's & prefs and run game

You will be able to hit the right shift key to activate run and left shift key to return to walk mode.

Don't worry, i am perfectly aware of how ineffecient this code is & don't acually expect anyone to use it. Just posting a little light humor ;p
#9
12/09/2005 (9:42 am)
Hmm compiling a file everytime the player changes speed...HA!

it's pretty funny that it actually works. that's like putting a bandaid on a broken leg. :)
#10
12/09/2005 (11:24 am)
I know, it works perfectly. Well as far as changing the player 'maxForwardSpeed' value. Obviously a function that changed this value upon command would be the wiser choice. :)
#11
12/10/2005 (6:57 am)
Such as...

function run()
{
datablock PlayerData(PlayerBody : Run)
{
maxForwardSpeed = 50;
};
}
#12
12/11/2005 (4:37 pm)
Well that works extremely well! Thanks very much Tim - excellent job indeed!

Tim - how about swapping out the animation to a different one for walk - any ideas?
#13
12/12/2005 (5:17 am)
Sorry Timlon, thats beyond my abilities. The code i gave you above came from an idea i had for basic fps style power ups. EG you pick up an item and change the datablock for jumpforce to allow a quick antigrav type feature. Such as...

function powerJump()
{
   MessageAll("","Jump Boost On");
   datablock PlayerData(PlayerBody : powerJump)
   {
   jumpForce = 8.3 * 900;
   };
schedule(10000, 0, jumpBoostoff );
}

function jumpBoostoff()
{
   MessageAll("","Jump Boost Off");
   datablock PlayerData(PlayerBody : powerJump)
   {
   jumpForce = 8.3 * 90;
   };
}

moveMap.bind( keyboard, lshift, powerJump );

As you can see this is a quick way to temporarily change any value of the playerbody datablock to anything you want. In the above code i schedule it to return to the default value after 10 seconds.

As far as changing the animation to a 'run' i'm not sure, sorry. I would think that would maybe require some head code alterations.
#14
12/12/2005 (8:09 pm)
Thanks for your help Tim, I never knew you could change the datablock in that way - most helpfull :)

As for having different animations for walk and run - I'll have a look through the engine source and see what I can see...I'm going in...wish me luck :)
#15
12/13/2005 (2:41 am)
Good luck, and be sure to check this thread.
#16
12/14/2005 (3:53 am)
Thanks Dirk - and nice link :)
#17
12/29/2005 (2:02 am)
Open player.h
After this
F32 maxFreelookAngle; ///< Max left/right angle the player can look

On add
// Adding new RunWalk 29/12/2004
F32 PercentRunWalkSpeed; ///< Percent Run Walk speed when Walk
// Adding new RunWalk 29/12/2004

Go to the class of player
class Player: public ShapeBase

On add
// Adding new RunWalk 29/12/2004
bool mPlayerRunWalk; // true=run, false=walk
// Adding new RunWalk 29/12/2004
Before this
Point3F mJumpSurfaceNormal; ///< Normal of the surface the player last jumped on


On the public:

Add this
// Adding new RunWalk 29/12/2004
void setPlayerRunWalk(bool runwalk);
bool getPlayerRunWalk() { return mPlayerRunWalk; }
// Adding new RunWalk 29/12/2004

After this function.
const char* getStateName();

Open player.cc
In PlayerData::PlayerData()
Add this

// Adding new RunWalk 29/12/2004
PercentRunWalkSpeed = 5;
// Adding new RunWalk 29/12/2004

In Player::Player()
Add this

// Adding new RunWalk 29/12/2004
mPlayerRunWalk= true; // true=run, false=walk
// Adding new RunWalk 29/12/2004

After this function.

ConsoleMethod( Player, checkDismountPoint, bool, 4, 4, "(Point3F oldPos, Point3F pos)")

On add
The Three functions


// Adding new RunWalk 29/12/2004
static S32 cgetPlayerRunWalk(SimObject *ptr, S32, const char **)
{
Player* obj = static_cast(ptr);
return obj->getPlayerRunWalk();
}
static void csetPlayerRunWalk(SimObject *ptr, S32, const char **argv)
{
Player* obj = static_cast(ptr);
obj->setPlayerRunWalk(dAtof(argv[2]));
}

void Player::setPlayerRunWalk(bool position)
{
mPlayerRunWalk = !mPlayerRunWalk;
}
// Adding new RunWalk 29/12/2004

In void Player::consoleInit()
On add

// Adding new RunWalk 29/12/2004
Con::addCommand("Player", "getPlayerRunWalk", cgetPlayerRunWalk, "obj.getPlayerRunWalk()", 2, 2);
Con::addCommand("Player", "setPlayerRunWalk", csetPlayerRunWalk, "obj.setPlayerRunWalk(runwalk)", 3, 3);
// Adding new RunWalk 29/12/2004
#18
12/29/2005 (2:09 am)
At the end of player.cc

ConsoleMethod( Player, getPlayerRunWalk, bool, 2, 2, "(true=run, false=walk)")
{
return object->getPlayerRunWalk();
}

ConsoleMethod( Player, setPlayerRunWalk, void, 3, 3, "(true=run, false=walk)")

{
object->setPlayerRunWalk(dAtof(argv[2]));
}
#19
12/29/2005 (2:21 am)
And in the function
void Player::updateMove(const Move* move)
change this

if (move->y > 0)
{
if( mWaterCoverage >= 0.9 )
moveSpeed = getMax(mDataBlock->maxUnderwaterForwardSpeed * move->y,
mDataBlock->maxUnderwaterSideSpeed * mFabs(move->x));
else
moveSpeed = getMax(mDataBlock->maxForwardSpeed * move->y,
mDataBlock->maxSideSpeed * mFabs(move->x));
}
else
{
if( mWaterCoverage >= 0.9 )
moveSpeed = getMax(mDataBlock->maxUnderwaterBackwardSpeed * mFabs(move->y),
mDataBlock->maxUnderwaterSideSpeed * mFabs(move->x));
else
moveSpeed = getMax(mDataBlock->maxBackwardSpeed * mFabs(move->y),
mDataBlock->maxSideSpeed * mFabs(move->x));
}

at this

if (move->y > 0)
{
if( mWaterCoverage >= 0.9 )
moveSpeed = getMax(mDataBlock->maxUnderwaterForwardSpeed * move->y,
mDataBlock->maxUnderwaterSideSpeed * mFabs(move->x));
else
{
if (getPlayerRunWalk())
moveSpeed = getMax(mDataBlock->maxForwardSpeed/mDataBlock->PercentRunWalkSpeed * move->y,
mDataBlock->maxSideSpeed/mDataBlock->PercentRunWalkSpeed * mFabs(move->x));
else
moveSpeed = getMax(mDataBlock->maxForwardSpeed * move->y,
mDataBlock->maxSideSpeed * mFabs(move->x));
}
}
else
{
if( mWaterCoverage >= 0.9 )
moveSpeed = getMax(mDataBlock->maxUnderwaterBackwardSpeed * mFabs(move->y),
mDataBlock->maxUnderwaterSideSpeed * mFabs(move->x));
else
{
if (getPlayerRunWalk())
moveSpeed = getMax(mDataBlock->maxBackwardSpeed/mDataBlock->PercentRunWalkSpeed * mFabs(move->y),
mDataBlock->maxSideSpeed/mDataBlock->PercentRunWalkSpeed * mFabs(move->x));
else
moveSpeed = getMax(mDataBlock->maxBackwardSpeed * mFabs(move->y),
mDataBlock->maxSideSpeed * mFabs(move->x));
}
}
#20
12/29/2005 (3:31 am)
Moussa: That is awesome! It worked perfectly - thank you very much indeed :)
Page «Previous 1 2