Increasing speed of Character Motor on collision help

Im trying to edit the CharacterMotor script for the FirstPersonController so that when the player collides with an object the players speed is increased.

Below is the edited part of the script that isnt working…

function OnControllerColliderHit (hit : ControllerColliderHit) {
if (hit.normal.y > 0 && hit.normal.y > groundNormal.y && hit.moveDirection.y < 0) {
	if ((hit.point - movement.lastHitPoint).sqrMagnitude > 0.001 || lastGroundNormal == Vector3.zero)
		groundNormal = hit.normal;
	else
		groundNormal = lastGroundNormal;
	
	movingPlatform.hitPlatform = hit.collider.transform;
	movement.hitPoint = hit.point;
	movement.frameVelocity = Vector3.zero;
     
	if(hit.gameObject.tag == "Boots") {
		 maxForwardSpeed = 100.0;
	}	 
}

}

The CharacterMotor is a very complicated script. Editing it isn’t a good idea - believe me, I tried a lot, and most times had only undesired side effects. In this case, maxForwardSpeed is just the speed limit, not the character speed. I suggest you to replace the original scripts with this one:

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

private var moveDirection : Vector3 = Vector3.zero;

function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = 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
controller.Move(moveDirection * Time.deltaTime);
}

It was extracted from:

http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html

It’s much easier to alter than the original CharacterMotor - just add this script to your character and disable CharacterMotor.js and FPSInputController.js.

Wow I love the script that Aldo Naletto posted. I’ve ported it to C# for those who are interested:

using UnityEngine;
using System.Collections;

public class CharacterMotor : MonoBehaviourOSP {
	
	public float speed = 6.0F;
	public float jumpSpeed = 8.0F;
	public float gravity = 20.0F;
	
	Vector3 moveDirection = Vector3.zero;
	
	new void Start () {
	
	}
	
	void Update() {
		CharacterController controller = gameObject.GetComponent<CharacterController>();
		
		
		if (controller.isGrounded == true) {
			// We are grounded, so recalculate
			// move direction 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
		controller.Move(moveDirection * Time.deltaTime);
	}
	
	
}