Changing Walking Speed In Game

I've used default FPS Contoller for my character. FPS camera view. So animations not problem. I just want to make sprint with shift key, i have coded it. Key script is ready. I made it into default "CharacterMotor.js" script.

I've tried to change "maxForwardSpeed" variable. It is not changing. I can't reach to variable. Or some other problem.

This variable in a "class" object, called like "CharacterMotorMovement", so maybe i need to add class adress to my function, but i don't know how to add a class adress to function.

Can someone help me? Thanks.

it is so long script, also it is default script for character controller, "CharacterMotor.js". You haven't it?

i just added these codes into "function update()" in this script:

///////////

class CharacterMotorMovement {

var maxForwardSpeed : float = 10.0;
var maxSidewaysSpeed : float = 10.0;
var maxBackwardsSpeed : float = 10.0;

}

function update() {

if (Input.GetKeyDown (KeyCode.LeftShift)) { maxForwardSpeed = 10; }

if (Input.GetKeyUp (KeyCode.LeftShift)) { maxForwardSpeed = 5; }

} //////////

Check this link, it has the anwser :slight_smile:

I guese oneone knows the anwser :D. I want to change it too :(

this is my running system system in c# so you will probably have to convert it (please note it isn’t the actual walking script I didn’t post the walking script because it is broke right now…I find my objects randomly going through colliders so I didn’t post it…however the run/autorun system still works fine it just needs to be adapted to fit your needs)

you will need the following variables

int curspeed;
int walkspeed;
int runspeed;
int autorun = 0;

		//if leftshift or rightshift is down
		if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
		{
			//current speed = runspeed
			curspeed = runspeed;
		}
		//if leftshift or rightshift is released
		if(Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
		{
			//if autorun is disabled
			if(autorun == 0)
			{
				//current speed = walkspeed
				curspeed = walkspeed;
			}
		}
		//if numlock is pressed (please note that autorun does not automatically move the player it only makes the player always run(like in oblivion for pc)
		if(Input.GetKeyDown(KeyCode.Numlock))
		{
			//if autorun is disabled
			if(autorun == 0)
			{
				//enable it
				autorun = 1;
				//current speed = runspeed
				curspeed = runspeed;
			}//if autorun is enabled
			else
			{
				//disable it
				autorun = 0;
				//current speed = walkspeed
				curspeed = walkspeed;
			}
		}

in your movement script curspeed will be the speed at which you will travel the above was made to edit the value of curspeed depending on what buttons are pressed

I hope this helps :slight_smile: (again I’m sorry my walking script is bugged or I would include that as well)

EDIT

fixed my walking script…(atleast I think) it is for a sidescroller but you should be able to easily adapt it to your needs with “rigidbody.velocity = transform.forward * curspeed;” assigning this to the W and KeyCode.UpArrow of course and -transform.forward for the s and DownArrow

by the way…you’ll need one more var

int isstuned = 0;

		//if the player isn't stunned
		if(isstuned == 0)
		{
			//if a or left arrow is pressed
			if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
			{
				//push left
				rigidbody.velocity = -transform.right * curspeed;
			}
			//if d or right arrow is pressed
			if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
			{
				//push right
				rigidbody.velocity = transform.right * curspeed;
			}
		}

this script causes the player to skid to a stop…but I kind of like it :slight_smile:

This is how you change it mid game in C#

CharacterMotor characterMotor = gameObject.GetComponent();
characterMotor.movement.maxForwardSpeed = speed;

Can translate this or download this lovely C# port that this brilliant individual has done: http://forum.unity3d.com/threads/64378-CharacterMotor-FPSInputController-PlatformInputController-in-C

If you want to access the characters script in order to change movement speed, jump, run speed and such, you will need to add a line of code on the top of your javascript that reads:

import UnityStandardAssets.Characters.FirstPerson;

then do your regular procedure to access other scripts such as:
var : Example : FirstPersonController;

Example = GameObject.Find(“Player”).GetComponent(FirstPersonController);

//Where player is the name of your FPS controller.

Also, you will need to go to the FirstPersonController script which is C# and change the Private variables that you want to change to Public. For example, the run speed is stated as
[SerializeField] Private float m_RunSpeed;
Simply change this to [SerializeField] public float m_RunSpeed;

This needs to be done since the variables are protected,

Well this is my solution!! I hope it helps! :slight_smile:

Sorry for digging, but it is the best suitable post I have found :wink:

My question @Will Squire:

But how to do

CharacterMotor characterMotor = gameObject.GetComponent(); characterMotor.movement.maxForwardSpeed = speed;

when C# don’t know CharacterMotor class (it is JavaScript as default Unity script)?

Only possibility is to write “enhancement moving” script in JS or port CharacterMotor to C#, yes?