Slider for FPS Movement Speed

Hello guys, 1 problem occur with my slider is when i trying to interrupt with the bar (left and right) and nothing change in CharacterMotor’s speed value. Is that any code I’m missing in the MoveSlider() ?

here the script:

/* Label and Slider Compound Control */

var Amount: int = 5;
var maxForwardSpeed;
var maxSidewaysSpeed;
var maxBackwardsSpeed;

function OnGUI ()
{

//movement speed

if (GUI.Button(Rect(3, 670, 20, 20), "-"))
{
gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed-1;
gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed-1;
gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed-1;
Amount--;
}

Amount = GUI.HorizontalScrollbar (Rect(28, 670, 100, 20), Amount, 5, 0, 600);

if (GUI.Button(Rect(133, 670, 20, 20), "+"))
{
gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed+1;
gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed+1;
gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed+1;
Amount++;
}
GUI.TextField (Rect(28, 690, 100, 20), String.Format ("{0:#.##}", gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed));

}

function MoveSlider() {

if (Amount--) { 
gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed-1;
gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed-1;
gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed-1;
  }

if (Amount++) {
gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed+1;
gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed+1;
gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed+1;
}

}

Thanks, I’m newbie.

if (Amount–) and if (Amount++) will always be evaluated as TRUE since Amount is an int (which does not evaluate to 0 or 1) instead of a boolean, so both if blocks are being called when MoveSlider() is called.

Try setting a flag for when your movement is changing to evaluate in your if block. Just to make it easy, I’m using an int in the example below that will always be either 1 or 0.

var int MoveSliderDown = 0; // starts as equivalent of where Amount-- was
//..other code declarations...

function OnGUI () {
  //movement speed

  if (GUI.Button(Rect(3, 670, 20, 20), "-")) {
      gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed-1;
      gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed-1;
      gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
      gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed-1;
      Amount--;
      MoveSliderDown = 0;
  }
  //<snip> 
  if (GUI.Button(Rect(133, 670, 20, 20), "+"))
  {
     gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
     gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed+1;
     gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
     gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed+1;
     gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
     gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed+1;
     Amount++;
     MoveSliderDown = 1;

}

function MoveSlider() {

   if (MoveSliderDown == 0)  {
     gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
     gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed-1;
     gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
     gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed-1;
     gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
     gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed-1;
   }

   if (MoveSliderDown == 1)  {
     gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed = 
     gameObject.GetComponent("CharacterMotor").movement.maxForwardSpeed+1;
     gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed = 
     gameObject.GetComponent("CharacterMotor").movement.maxSidewaysSpeed+1;
     gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed = 
     gameObject.GetComponent("CharacterMotor").movement.maxBackwardsSpeed+1;
   }
}

It would be better to use a boolean flag, but my brain’s not working correctly right now :slight_smile: