Game Development Community

Modifying player speed

by Jon C · in Torque Game Engine · 03/09/2007 (3:56 pm) · 18 replies

I have been looking around for how to do this, however most of the posts are several years old... So I thought I would renew this topic.

How would you modify a player's speed, via an object or weapon? Such as a player getting hit, then becoming faster or slower.

#1
03/09/2007 (4:58 pm)
I would say look at player.cs

function Armor::damage or function Armor::onDamage

then change the players speed (and I don't know if this is the best way but I just tested it in a single player and it worked): %this.maxForwardSpeed = 2; // change the players speed

then you have to change it back after a few seconds using a function and a schedule that calls that function...


or something like that
#2
03/09/2007 (5:06 pm)
Btw: a quick test is to change the water. So when you enter you slow down, then when you leave you speed back up. Just thought I'd share how I tested that.

server/scripts/player.cs
function Armor::onEnterLiquid(%this, %obj, %coverage, %type)
{
switch(%type)
{
case 0: //Water
%this.maxForwardSpeed = 2;
case 1: //Ocean Water
%this.maxForwardSpeed = 2;

case 2: //River Water
case 3: //Stagnant Water
case 4: //Lava
%obj.setDamageDt(%this, $DamageLava, "Lava");
case 5: //Hot Lava
%obj.setDamageDt(%this, $DamageHotLava, "Lava");
case 6: //Crusty Lava
%obj.setDamageDt(%this, $DamageCrustyLava, "Lava");
case 7: //Quick Sand
}
}

function Armor::onLeaveLiquid(%this, %obj, %type)
{
%obj.clearDamageDt();
%this.maxForwardSpeed = 6;
}
#3
03/09/2007 (5:10 pm)
Thanks, but I'm actually trying to do it with indiviual weapons... Heres what I have in crossbow:

function CrossbowProjectile::onCollision(%this,%obj,%col,%fade,%pos,%normal)
{
// Apply damage to the object all shape base objects
if (%col.getType() & $TypeMasks::ShapeBaseObjectType)
%col.damage(%obj,%pos,%this.directDamage,"CrossbowBolt");

//Slow player down
%obj.maxForwardSpeed = 0.1;

// Radius damage is a support scripts defined in radiusDamage.cs
// Push the contact point away from the contact surface slightly
// along the contact normal to derive the explosion center. -dbs
radiusDamage(%obj, %pos, %this.damageRadius, %this.radiusDamage, "Radius", %this.areaImpulse);
}



Its not working... Can you not modify an object's speed?
#4
03/09/2007 (5:14 pm)
Armor is a dataBlock doing this will change all the client's speed using the same dataBlock.

Aun.
#5
03/09/2007 (5:15 pm)
Function Armor::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)

I think the %sourceObject is what you would use in a switch statement or something.

like:
switch(%sourceObject )
{
      case crossbow:  // I dont know what is returned here but change it accordingly...
         %this.maxForwardSpeed = 0.1;
}
#6
03/09/2007 (5:20 pm)
Switch(%sourceObject)
{
case "CrossbowBolt": // I dont know what is returned here but change it accordingly...
%this.maxForwardSpeed = 0.1;
}

Thanks, that ended up working... However, how do you create/use timers...? And Aun, after shooting the test NPC in starter.fps with this, it slowed him down but didn't slow me down.
Are you sure the datablock is shared?



Actually, do "damage over time" damage types exist in .cs files? If so, couldn't I just create one for this?
#7
03/09/2007 (5:24 pm)
Might wanna test it in multiplayer.
#8
03/09/2007 (5:27 pm)
ShapeBase.cs

Function ShapeBase::setDamageDt(%this, %damageAmount, %damageType)
{
// This function is used to apply damage over time.
#9
03/09/2007 (5:37 pm)
You can use a weapons 'mass' to slow/speed the player.
#10
03/09/2007 (8:00 pm)
Yes, I'm sure dataBlocks are shared across the network (that is if you use the same datablock to create a player , npc or Ai).

switch(%sourceObject ){      
    case crossbow:  // I dont know what is returned here but change it accordingly...         
         %this.maxForwardSpeed = 0.1;
}

It'll work if you are creating a single player game, but won't be a good pratice if you want multiplayer.
DataBlock are meant to be shared with many objects to reduce the need of sending alot of datas across the network between the server and client. If you want to change the speed of a player I recommend not changing its dataBlock.

PS: crap I just found it been discussed beforehere

EDIT* = TM-Document have some document about changing the player's speed while walking you could have a look there.

Aun.
#11
03/10/2007 (9:08 am)
Thanks I added that, and it compiled fine however I am trying to figure out how to get my slow function to work correctly... How would I call it?

function ShapeBase::setSlowDt(%this, %slowAmount, %slowType)
{
   if (%obj.getState() !$= "Dead") {
	if (!$slowOn) {
		%obj.runForce = 0.5;
		%this.runForce = 0.5;
		$slowOn = 1;
		%obj.slowSchedule = %obj.schedule(5000, "setSlowDt", %slowAmount, %slowType);
	} else {
		%obj.runForce = 10.0;
		%this.runForce = 10.0;
		$slowOn = 0;
		%obj.slowSchedule = "";
	}
   }
   else
      %obj.slowSchedule = "";
}
Yes I know, I'm using %obj and %this... But I honestly don't know which is which... And I just want to make sure this works



I'm calling it in function CrossbowProjectile::onCollision(%this,%obj,%col,%fade,%pos,%normal):
%obj.setSlowDt(%obj, "10", "ice");



Also, just out of curiosity... Is %obj a pointer to a global variable/array/class that is dedicated to the person/object its being called forth from? If so, would it be better to change $slowOn to %obj.slowOn?
#12
03/10/2007 (7:53 pm)
I went into game and checked console... I kept getting "Unknown function setSlowDt" or something along those lines. Even if I took out shapeBase::, and put the function in crossbow.cs
#13
03/10/2007 (7:56 pm)
Trying calling this instead


%obj.setSlowDt( 10, "ice");

Aun.

#14
03/10/2007 (8:00 pm)
Nope... Still "Unknown command setSlowDt"
Where exactly should I put the function?
#15
03/11/2007 (9:48 pm)
%this refers to the ID of whatever called the function, it can actually be named anything you want. %this is always the first argument and I think %obj is 2nd?


so maybe you can put:

function ShapeBase::setSlowDt(%this, %obj, %slowAmount, %slowType)

if that doesnt work, try adding an echo to print out what %this is in the console.

like:

echo ("this: " @ %this);
echo ("this.dataBlock: " @ %this.getDatablock());
echo ("obj: " @ %obj);


then use the ID# and dump() like: 4355.dump(); and it will show all the functions available to it.
#16
03/14/2007 (5:28 pm)
Sorry if this is not relevant, but wouldn't changing the speed make it inconsistent with the animation? Or does Torque modify the animation accordingly?
#17
03/14/2007 (8:33 pm)
@Skyoii : I would add a console function to the Player class in the engine to access the "runForce" variable.

@Hasitha : Yes, It would make the animations inconsistent with the speed.

Aun.

Torque-Motion
#18
03/15/2007 (6:32 am)
@Skyoii: I forgot to mention this but there is a mod that creates a sprint button which you might be able change for what you need. Click Here for Sprint Resource.

@Hasitha, Aun: when I've modified the speed (using the resource mentioned above) the animations automatically slowed or sped up.