Running

How do i make a running thing like Mw2 where you can only run for a certain time? It also obviously has to increase my speed. Thanks.

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

function FixedUpdate() {
	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
	}

	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;
}


@script RequireComponent(CharacterController)

This code will modify what you have so you start running, but slow to a walk after 20.0 seconds. When not moving, you rest so you can run again. The longer you rest the longer you can run again, up to 20 seconds rest for 20 seconds running.

The code isn’t tested, but you could give her a go!

var runSpeed = 6.0;
var walkSpeed = 3.0;
var speed;
var maxRunTime = 20.0; // can run for 20 seconds.

   if (grounded) {
      // We are grounded, so recalculate movedirection directly from axes
      moveDirection = new 
// check to see if movement buttons are being pressed (if so  we are getting tired, even if walking):
if(Input.GetAxis("Horizontal") || Input.GetAxis("vertical))
{
runningTime += Time.deltaTime;    //getting tired
} else {
// if not pressing a movement button, then we are resting:
runningTime -= Time.deltaTime;   // resting
}

if(runningTime > maxRunTime) // are we too tired to run?
{
speed = walkSpeed;     // then we so to a walk speed.
runningTime = maxRunTime  // so we don't get more tired from walking
}
else speed = runSpeed;
}

Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
      moveDirection = transform.TransformDirection(moveDirection);
      moveDirection *= speed;
      
      if (Input.GetButton ("Jump")) {
         moveDirection.y = jumpSpeed;
      }
   }

It says, "unexpected token ‘new’

Here, try this:

var jumpSpeed = 8.0;
var gravity = 20.0;
var runSpeed = 6.0;
var walkSpeed = 3.0;
var speed;
var runningTime = 0.0;
var maxRunTime = 20.0; // can run for 20 seconds.

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

function FixedUpdate() {
   if (grounded) {
	// check to see if movement buttons are being pressed (if so  we are getting tired, even if walking):
	if(Input.GetAxis("Horizontal") || Input.GetAxis("vertical"))
	{
		runningTime += Time.deltaTime;    //getting tired
	} else {
		// if not pressing a movement button, then we are resting:
		runningTime -= Time.deltaTime;   // resting
	}

	if(runningTime > maxRunTime) // are we too tired to run?
	{
		speed = walkSpeed;     // then we so to a walk speed.
		runningTime = maxRunTime;  // so we don't get more tired from walking
	}
	else speed = runSpeed;

      // We are grounded, so recalculate movedirection directly from axes
      moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= speed;
     
      if (Input.GetButton ("Jump")) {
         moveDirection.y = jumpSpeed;
      }
   }

 // Apply gravity
   moveDirection.y -= gravity * Time.deltaTime;
   
   // Move the controller
   var controller : CharacterController = GetComponent(CharacterController);
   var flags = controller.Move(moveDirection * Time.deltaTime);
   grounded = (flags  CollisionFlags.CollidedBelow) != 0;
}


@script RequireComponent(CharacterController)

I tried to figure this out, but how do i initiate the running? Is this running like in a game were you press a button(E) in my case, and run for a little?

watch that for the running i am talking about.

All I did was modify your existing script so that after 20 seconds, your speed slows to a walk. If you stop moving, you become more rested and can run again.

If the script you posted worked, then I just added that one thing.

That’s what I thought you were asking for. The rest is up to you.