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;
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.
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.
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
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?
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:
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.