Miscellaneous Gameplay Features

12 min read

(For more resources related to this topic, see here.)

How to have a sprinting player use up energy

Torque 3D’s Player class has three main modes of movement over land: sprinting, running, and crouching. Some are designed to allow a player to sprint as much as they want, but perhaps with other limitations while sprinting. This is the default method of sprinting in the Torque 3D templates. Other game designs allow the player to sprint only for short bursts before the player becomes “tired”. In this recipe, we will learn how to set up the Player class such that sprinting uses up a pool of energy that slowly recharges over time; and when that energy is depleted, the player is no longer able to sprint.

How to do it…

We are about to modify a PlayerData Datablock instance so that sprint uses up the player’s energy as follows:

  1. Open your player’s Datablock in a text editor, such as Torsion. The Torque 3D templates have the DefaultPlayerData Datablock template in art/ datablocks/player.cs.

  2. Find the sprinting section of the Datablock instance and make the following changes:

    sprintForce = 4320; sprintEnergyDrain = 0.6; // Sprinting now drains energy minSprintEnergy = 10; // Minimum energy to sprint maxSprintForwardSpeed = 14; maxSprintBackwardSpeed = 8; maxSprintSideSpeed = 6; sprintStrafeScale = 0.25; sprintYawScale = 0.05; sprintPitchScale = 0.05; sprintCanJump = true;

  3. Start up the game and have the player sprint. Sprinting should now be possible for about 5.5 seconds before the player falls back to a run.

  4. If the player stops sprinting for about 7.5 seconds, their energy will be fully recharged and they will be able to sprint again.

How it works…

The maxEnergy property on the PlayerData Datablock instance determines the maximum amount of energy a player has. All of Torque 3D’s templates set it to a value of 60. This energy may be used for a number of different activities (such as jet jumping), and even certain weapons may draw from it.

By setting the sprintEnergyDrain property on the PlayerData Datablock instance to a value greater than zero, the player’s energy will be drained every tick (about one-thirty-second of a second) by that amount. When the player’s energy reaches zero they may no longer sprint, and revert back to running.

Using our previous example, we have a value for the sprintEnergyDrain property of 0.6 units per tick. This works out to 19.2 units per second. Given that our DefaultPlayerData maxEnergy property is 60 units, we should run out of sprint energy in 3.125 seconds. However, we were able to sprint for about 5.5 seconds in our example before running out of energy. Why is this?

A second PlayerData property affects energy use over time: rechargeRate. This property determines how much energy is restored to the player per tick, and is set to 0.256 units in DefaultPlayerData. When we take both the sprintEnergyDrain and recharcheRate properties into account, we end up with an effective rate of (0.6 – 0.256) 0.344 units drained per tick while sprinting. Assuming the player begins with the maximum amount of energy allowed by DefaultPlayerData, this works out to be (60 units / (0.344 units per tick * 32 ticks per second)) 5.45 seconds.

The final PlayerData property that affects sprinting is minSprintEnergy. This property determines the minimum player energy level required before being able to sprint. When this property is greater than zero, it means that a player may continue to sprint until their energy is zero, but cannot sprint again until they have regained a minSprintEnergy amount of energy.

There’s more…

Let’s continue our discussion of player sprinting and energy use.

Balance energy drain versus recharge rate

With everything set up as described previously, every tick the player is sprinting his energy pool will be reduced by the value of sprintEnergyDrain property of PlayerData, and increased by the value of the rechargeRate property. This means that in order for the player’s energy to actually drain, his sprintEnergyDrain property must be greater than his rechargeRate property.

As a player’s energy may be used for other game play elements (such as jet jumping or weapons fire), sometimes we may forget this relationship while tuning the rechargeRate property, and end up breaking a player’s ability to sprint (or make them sprint far too long).

Modifying other sprint limitations

The way the DefaultPlayerData Datablock instance is set up in all of Torque 3D’s templates, there are already limitations placed on sprinting without making use of an energy drain. This includes not being able to rotate the player as fast as when running, and limited strafing ability. Making sprinting rely on the amount of energy a player has is often enough of a limitation, and the other default limitations may be removed or reduced. In the end it depends on the type of game we are making.

To change how much the player is allowed to rotate while sprinting, we modify the sprintYawScale and sprintPitchScale properties of the PlayerData property. These two properties represent the fraction of rotation allowed while sprinting compared with running and default to 0.05 each.

To change how much the player is allowed to strafe while sprinting, we modify the sprintStrafeScale property of the PlayerData property. This property is the fraction of the amount of strafing movement allowed while running and defaults to 0.25.

Disabling sprint

During a game we may want to disable a player’s sprinting ability. Perhaps they are too injured, or are carrying too heavy a load. To allow or disallow sprinting for a specific player we call the following Player class method on the server:

Player.allowSprinting( allow );

In the previous code, the allow parameter is set to true to allow a player the ability to sprint, and to false to not allow a player to sprint at all.

This method is used by the standard weapon mounting system in scripts/server/ weapon.cs to disable sprinting. If the ShapeBaseImageData Datablock instance for the weapon has a dynamic property of sprintDisallowed set to true, the player may not sprint while holding that weapon. The DeployableTurretImage Datablock instance makes use of this by not allowing the player to sprint while holding a turret.

Enabling and disabling air control

Air control is a fictitious force used by a number of games that allows a player to control their trajectory while falling or jumping in the air. Instead of just falling or jumping and hoping for the best, this allows the player to change course as necessary and trades realism for playability. We can find this type of control in first-person shooters, platformers, and adventure games. In this recipe we will learn how to enable or disable air control for a player, as well as limit its effect while in use.

How to do it…

We are about to modify a PlayerData Datablock instance to enable complete air control as follows:

  1. Open your player’s Datablock in a text editor, such as Torsion. The Torque 3D templates have the DefaultPlayerData Datablock instance in art/ datablocks/player.cs.

  2. Find the section of the Datablock instance that contains the airControl property and make the following change:

    jumpForce = "747"; jumpEnergyDrain = 0; minJumpEnergy = 0; jumpDelay = "15"; // Set to maximum air control airControl = 1.0;

  3. Start up the game and jump the player off of a building or a sand dune. While in the air press one of the standard movement keys: W, A, S, and D.

We now have full trajectory control of the player while they are in the air as if they were running.

How it works…

If the player is not in contact with any surface and is not swimming, the airControl property of PlayerData is multiplied against the player’s direction of requested travel. This multiplication only happens along the world’s XY plane and does not affect vertical motion.

Setting the airControl property of PlayerData to a value of 0 will disable all air control. Setting the airControl property to a value greater than 1 will cause the player to move faster in the air than they can run.

How to jump jet

In game terms, a jump jet is often a backpack, a helicopter hat, or a similar device that a player wears, that provides them a short thrust upwards and often uses up a limited energy source. This allows a player to reach a height they normally could not, jump a canyon, or otherwise get out of danger or reach a reward. In this recipe we will learn how to allow a player to jump jet.

Getting ready

We will be making TorqueScript changes in a project based on the Torque 3D Full template using the Empty Terrain level. If you haven’t already, use the Torque Project Manager (Project Manager.exe) to create a new project from the Full template. It will be found under the My Projects directory. Then start up your favorite script editor, such as Torsion, and let’s get going!

How to do it…

We are going to modify the player’s Datablock instance to allow for jump jetting and adjust how the user triggers the jump jet as follows:

  1. Open the art/datablocks/player.cs file in your text editor.

  2. Find the DefaultPlayerData Datablock instance and just below the section on jumping and air control, add the following code:

    // Jump jet jetJumpForce = 500; jetJumpEnergyDrain = 3; jetMinJumpEnergy = 10;

  3. Open scripts/main.cs and make the following addition to the onStart() function:

    function onStart() { // Change the jump jet trigger to match a regular jump $player::jumpJetTrigger = 2; // The core does initialization which requires some of // the preferences to loaded... so do that first. exec( "./client/defaults.cs" ); exec( "./server/defaults.cs" ); Parent::onStart(); echo("n--------- Initializing Directory: scripts ---------"); // Load the scripts that start it all... exec("./client/init.cs"); exec("./server/init.cs"); // Init the physics plugin. physicsInit(); // Start up the audio system. sfxStartup(); // Server gets loaded for all sessions, since clients // can host in-game servers. initServer(); // Start up in either client, or dedicated server mode if ($Server::Dedicated) initDedicated(); else initClient(); }

  4. Start our Full template game and load the Empty Terrain level.

  5. Hold down the Space bar to cause the player to fly straight up for a few seconds. The player will then fall back to the ground. Once the player has regained enough energy it will be possible to jump jet again.

How it works…

The only property that is required to be set for jump jetting to work is the jetJumpForce property of the PlayerData Datablock instance. This property determines the amount of continuous force applied on the player object to have them flying up in the air. It takes some trial and error to determine what force works best.

Other Datablock properties that are useful to set are jetJumpEnergyDrain and jetMinJumpEnergy. These two PlayerData properties make jet jumping use up a player’s energy. When the energy runs out, the player may no longer jump jet until enough energy has recharged. The jetJumpEnergyDrain property is how much energy per tick is drained from the player’s energy pool, and the jetMinJumpEnergy property is the minimum amount of energy the player needs in their energy pool before they can jump jet again. Please see the How to have a sprinting player use up energy recipe for more information on managing a player’s energy use.

Another change we made in our previous example is to define which move input trigger number will cause the player to jump jet. This is defined using the global $player::jumpJetTrigger variable. By default, this is set to trigger 1, which is usually the same as the right mouse button. However, all of the Torque 3D templates make use of the right mouse button for view zooming (as defined in scripts/client/default.bind.cs).

In our previous example, we modified the global $player::jumpJetTrigger variable to use trigger 2, which is usually the same as for regular jumping as defined in scripts/ client/default.bind.cs:

function jump(%val) { // Touch move trigger 2 $mvTriggerCount2++; } moveMap.bind( keyboard, space, jump );

This means that we now have jump jetting working off of the same key binding as regular jumping, which is the Space bar. Now holding down the Space bar will cause the player to jump jet, unless they do not have enough energy to do so. Without enough energy, the player will just do a regular jump with their legs.

There’s more…

Let’s continue our discussion of using a jump jet.

Jump jet animation sequence

If the shape used by the Player object has a Jet animation sequence defined, it will play while the player is jump jetting. This sequence will play instead of all other action sequences. The hierarchy or order of action sequences that the Player class uses to determine which action sequence to play is as follows:

  1. Jump jetting

  2. Falling

  3. Swimming

  4. Running (known internally as the stand pose)

  5. Crouching

  6. Prone

  7. Sprinting

Disabling jump jetting

During a game we may no longer want to allow a player to jump jet. Perhaps they have run out of fuel or they have removed the device that allowed them to jump jet. To allow or disallow jump jetting for a specific player, we call the following Player class method on the server:

Player.allowJetJumping( allow );

In the previous code, the allow parameter is set to true to allow a player to jump jet, and to false for not allowing him to jump jet at all.

More control over the jump jet

The PlayerData Datablock instance has some additional properties to fine tune a player’s jump jet capability. The first is the jetMaxJumpSpeed property. This property determines the maximum vertical speed at which the player may use their jump jet. If the player is moving upwards faster than this, then they may not engage their jump jet.

The second is the jetMinJumpSpeed property. This property is the minimum vertical speed of the player before a speed multiplier is applied. If the player’s vertical speed is between jetMinJumpSpeed and jetMaxJumpSpeed, the applied jump jet speed is scaled up by a relative amount. This helps ensure that the jump jet will always make the player move faster than their current speed, even if the player’s current vertical speed is the result of some other event (such as being thrown by an explosion).


These recipes will help you to fully utilize the gameplay’s features and make your game more interesting and powerful. The tips and tricks mentioned in the recipes will surely help you in making the game more real, more fun to play, and much more intriguing.

Resources for Article :

Further resources on this subject:


Please enter your comment!
Please enter your name here