Editing First Person Controller movement / jumping variables from C#

Hello everyone,

I am wanting to dynamically modify the movement and jumping values of the standard Character Motor script from another script that is in C#, within the same prefab.

Anyone know how to achieve this?

bump. May have to write my own controller or something :expressionless:

Well the way i would do that is by adding some custom functions in the code to set each value kinda like i didn’t with setControl and setJump in this snippet

var useFixedUpdate : boolean = true;

///begin custom code
function setControl(can : boolean)
{
    canControl = can;
}
function setJump(can : boolean)
{
jumping.enabled = can;
}
//end custom code

then to call them all you have to do is

obj.SendMessage("setJump",true);

obj is the object that your controller componenent is attached to.

awesome, cheers. I’ve no clue how java script works in Unity, so I had no damned clue how to make it work or whichwhatever.

Thank you very much :slight_smile:

I have an error where it says I need a receiver for it to function. Any ideas as to how to implement it? I’m looking at the documentation now and I haven’t the faintest.

The third parameter of SendMessage is SendMessageOptions and it is SendMessageOptions.RequireReceiver as default value. Probably your destination gameObject hasn’t a script with a method named “setJump” and so it raises that error, but you can specify SendMessageOptions.DontRequireReceiver to avoid it if you aren’t sure that the gameObject hasn’t such script/method.

Cheers. I made a method setmovespeed(speed : float) before posting. I’ve now added what you suggested and it doesn’t seem to work. I must be putting the function in the wrong place. as the function does not run. I’ve put it just below the awake() in the default first person charactermotor.

How I am calling the message in C# is this:

void Start () 
	{
		Instance = this;
initPlayer();
}

private void initPlayer()
	{
		// Enter here what affects the player's move speed etc.
		Debug.Log(_speed);
		Instance.SendMessage("setMoveSpeed", _speed); // SendMessageOptions.DontRequireReceiver
	}

I commented out the listener options purposefully, and it gives no errors.

Assuming that your script is attached to the same gameObject that has the CharacterMotor script:

  1. your script could be like:
using UnityEngine;
using System.Collections;

public class YourScriptName : MonoBehaviour {

	private float _speed = .0f; // adjust this value or make it public to set in inspector

	void Start () 
	{
		initPlayer();
	}

	void initPlayer()
	{
		SendMessage("setMoveSpeed", _speed);
	}
}
  1. your CharacterMotor script should have this method (be aware to have the same letter-case as SendMessage because it is of course case-sensitive), it has no clue where you write this method in the JS:
function setMoveSpeed(speed : float) {
	print("speed set to " + speed);
}

Hurray! I got it! Thanks for the help!

For those wondering.

I put the public function setMoveSpeed(speed : float) at the very bottom of the motor.cs, just below ‘setvelocity’.

and called it from the C# code ‘SendMessage(“setMoveSpeed”, _speed);’

Thanks again.