- Home /
How to modify the base movement speed of the character in JS
I want to have the character level up, and his movement speed increase with each level.
function increaseLevel(){
level = level+1;
maxForwardSpeed = maxForwardSpeed+(maxForwardSpeed/5);
maxBackwardSpeed = maxBackwardSpeed+(maxBackwardSpeed/5);
maxSidewaySpeed = maxSidewaySpeed+(maxSidewaySpeed/5);
}
increaseLevel is called earlier in the script, but that's not hat I'm worried about. I get the error "Unknown Identifier: maxForwardSpeed" (or backward/sideway) for obvios reasons, those variables are not declared anywhere but the CharacterMotor script (I'm using the default FPS character motor). How would I make it so the movement speed would increase? Do I have to write my own Character Motor script? Could I 'cross-reference' the variables so they are declared in CharacterMotor.js and are changed in my script?
The last one I expect should work; use GetComponent references to C$$anonymous$$otor and adjust values in your script
Answer by SkaredCreations · Mar 22, 2014 at 05:27 PM
Here is, just drag the object of your CharacterMotor into the property "motor":
#pragma strict
var motor : CharacterMotor;
private var level : int = 0;
function increaseLevel(){
level = level+1;
motor.movement.maxForwardSpeed = motor.movement.maxForwardSpeed+(motor.movement.maxForwardSpeed/5);
motor.movement.maxBackwardsSpeed = motor.movement.maxBackwardsSpeed+(motor.movement.maxBackwardsSpeed/5);
motor.movement.maxSidewaysSpeed = motor.movement.maxSidewaysSpeed+(motor.movement.maxSidewaysSpeed/5);
}
function Update () {
if (Input.GetKeyUp(KeyCode.A)) {
increaseLevel();
}
}
I think under my circumstances the solution was a little different, this worked with a few small tweaks.
var level : int;
var inBox : boolean;
var motor : Character$$anonymous$$otor;
function OnTriggerEnter(other : Collider) {
if(other.tag == "Player"){
inBox = true;
}
}
function Update () {
if (Input.GetButtonDown("Fire1")) {
increaseLevel();
}
}
function increaseLevel(){
level = level+1;
motor.movement.maxForwardSpeed = motor.movement.maxForwardSpeed+(motor.movement.maxForwardSpeed/5);
motor.movement.maxBackwardsSpeed = motor.movement.maxBackwardsSpeed+(motor.movement.maxBackwardsSpeed/5);
motor.movement.maxSidewaysSpeed = motor.movement.maxSidewaysSpeed+(motor.movement.maxSidewaysSpeed/5);
}
This is what I ended up with. Aside from my math making for quite a fast character at level 30, it works like a charm. Thank you!