Game Development Community

Using the shift key to run

by Manoel Neto · in Torque Game Engine · 01/24/2005 (10:41 am) · 28 replies

Hi,

I've been trying to get the shift key to double the movement speed for hours, to no avail. I just cannot bind anything to the shift keys, the functions are simply not called. I tried creating shift* versions of the move functions and mapping those to "shift a", "shift d", etc, but it doesn't work properly (pressing shift while walking won't change the speed).

How do I get the shift key to trigger a bind function?
Page«First 1 2 Next»
#21
03/30/2005 (1:44 pm)
Only problem i see with the throttle code is when you let go of a direction you keep going in that direction,

you have to HOLD down the reverse direction for over 5 seconds to come to a complete stop
#22
03/30/2005 (3:25 pm)
Ed,

Yes, your code is close, but I think you want:
$walkSpeed = 0.6; // walk speed
$runSpeed = 1.0; // run speed
$running = false; // initialize variable
$speedRate = 31; // how often it is updated (every tick)
function updateSpeed() {
  if ($movementDirection > 0) { // if moving forward
     if ($running) {
       $movementSpeed = $runSpeed;
     } else {
       $movementSpeed = $walkSpeed;
    }
  }
  $mvForwardAction = $movementSpeed;
  if (isEventPending($SpeedSchedule))
    cancel($SpeedSchedule);
  $SpeedSchedule = schedule( $speedRate, 0, "updateSpeed");
}

function stop() {
  $movementSpeed = 0;
  $mvForwardAction = $movementSpeed;
  if (isEventPending($SpeedSchedule))
    cancel($SpeedSchedule);
}

function toggleRun(%val) {
  if (%val) {
    $running = true;
  } else {
    $running = false;
  }
}
moveMap.bind(keyboard, "shift", toggleRun);

function moveForward(%val) {
  if (%val) {
    $movementdirection = 1; // forward
    updateSpeed();
  } else {
    stop();
  }
}
moveMap.bind(keyboard, "up", moveForward);

function moveBackward(%val) {
  if (%val) {
    $mvForwardAction = -$walkSpeed;
  } else {
    stop();
  }
}
moveMap.bind(keyboard, "down", moveBackward);

No guarantees that it will work exactly as shown, I haven't tested it. Your code only changes how fast the speed changes (i.e. the acceleration) above has removed the "smooth" transition but keep the checking.

As for your second comment, that's how a throttle works. This code won't do that. And the time to stop is based on the increment values, higher values are faster changes. This code is instantaneous.

HIH
#23
03/30/2005 (7:26 pm)
Hmmmm nothin happens when i press shift...
#24
03/30/2005 (7:55 pm)
My code works. And regardless of the method, I think Torque handles acceleration without requiring explicit speedup or speedown functions.
#25
03/30/2005 (9:02 pm)
Yeah, i dont mind modding the engine, especially for core functionality (player locomotion)
#26
03/31/2005 (3:07 pm)
Just a thought but isn't there an OnKeyDown and OnKeyUp event handlers for torque?

If there is you could just do this

LShift::OnKeyDown(){
    CommandToServer(PlayerSpeed,"RUN");
}

LShift::OnKeyUP(){
   CommandToServer(PlayerSpeed,"WALK");
}
Then add the following to server/commands.cs
ServerCmdPlayerSpeed(%client,%speedvar){
       if(%speedvar $="RUN"){
             %client.runForce *= 2;
             %client.maxForwardSpeed *= 2;
             %client.maxBackwardSpeed *= 2;
             %client.maxSideSpeed *= 2;
      }else{
            %client.runForce /= 2;
             %client.maxForwardSpeed /= 2;
             %client.maxBackwardSpeed /= 2;
             %client.maxSideSpeed/*= 2;
     }
}

Anyways thats how I would do it, but that would require KeyPress event handlers :)
#27
03/31/2005 (3:20 pm)
@Dreamer. That's what %val is for. %val==true on keypress, %val==false on keyrelease.
#28
03/31/2005 (3:22 pm)
Oh sweet that makes the code even cleaner
Page«First 1 2 Next»