Unity 3 beta CharacterMotor help!

This is of course aimed at those who also have the beta, although other’s might be able to answer it but fat chance.

I’m trying to increase speed for a sprint action, similar to those seen in Call of Duty, and other similar first person shooters. But I’m doing this as a sort of equip able perk in a separate script to the one that controls speed. Normally I can do this relatively easily, but the fact of the matter is the new CharacterMotor.js won’t let me.

How does one go about accessing the ‘maxForwardSpeed’ variable in another script, keeping in mind that it’s inside a ‘class’. And yes, I’ve tried making the variable static, I’m unfamiliar with the concept of classes in JavaScript, if someone could so kindly answer my question or nudge me in the right direction, that would be brilliant.

class CharacterMotorMovement {
	static var maxForwardSpeed : float = 8.0;

That’s the part I’m talking about…

You could make a public function inside the class increasing the variable…

I tried that, now it’s giving me crap about how; ‘enabled’ is not a member of ‘UnityEngine.Component’.

Here’s the function I used to test.

public function sprintingActive() {
		if (Perks.sprintActive) {
			maxForwardSpeed = maxForwardSpeed + 6.0;
		}
	}

I’m able to access my variable ‘sprintActive’ which is a boolean from other scripts, just not this one, it seems CharacterMotor.js is a jerk.
Thanks anyway man.

Which means that you are trying to enable or disable the script in a wrong way…

Well that’s just how I’ve done it in the past… but that being said I’m no JavaScript expert, I normally just script stuff to do with the GUI.

Any suggestions? :lol:

I’ve totally lost track on the problem :wink: What is it you want to achieve and what is your current ‘solution’ ?

Haha XD I want to be able to change the variable maxForwardSpeed from CharacterMotor.js in another script. I did try mixing things up a little and tried to access a boolean from my script that handles perks. That is what gave me the error that I mentioned a few posts back.

Here is a good read to start with:

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Basically, it comes down to finding your object (if the script is on another gameobject) and then to a simple GetComponent(CharacterMotor). You can then either call for the public variable itself or create a function in your CharacterMotor class that changes it.

I can’t really go into the specifics because I’m a C# programmer, and GetComponent works a little different in JS. But Get back here when you are really stuck again :smile:

I’m having trouble accessing the gravity variable from the character movement class. In my script, I access the gravity through CharacterMotor.gravity, but it throws an error saying that gravity isn’t a member of character motor.

How does one access a variable in a class from another script?

Is it public and inside the class? Maybe you should try renaming the variable because gravity is also used for physics based controllers in Unity…

I figured it out. You make a variable that references the class.

var movement : CharacterMotorMovement = CharacterMotorMovement();

As for gravity, movement.gravity is different than physx’s gravity. :slight_smile:

Thanks a lot! To be completely honest I’d given up on this, but I will now be giving this a try.

Thanks for your help, but I wanted to mention that the script above is the Constructor for the CharacterMotorMovement class. So, while it can construct a new CharacterMotorMovement class, it cannot change the player’s maxForwardSpeed directly.

So, if you want to change the maxForwardSpeed from the CharacterMotor script on the player once the game has begun, you will need to use the following in your script in order to get it:

gameObject.GetComponent(CharacterMotor).movement.maxForwardSpeed;

This has been tested by myself, so I know for a fact that it works.

Note:
the gameObject above is your player… your script should be attached to your player character in most cases.
If you don’t want to attach the script to your player, put the following into your script:
gameObject.FindWithTag(“Player”).GetComponent(CharacterMotor).movement.maxForwardSpeed;
…Although I am not sure if that last one will work or not in all cases

Thanks for all the help on this thread. I didn’t see a formal answer on here and still ended up taking forever to figure out how to manipulate the maxForwardSpeed, so I thought I’d add to the thread something that works.