- Home /
How to change maxForwardSpeed from C#
I need to increase character's max speed and max jump height on certain conditions. How do I access the properties of CharacterMotor from C#? I have this script attached to the Character Controller: using UnityEngine; using System.Collections;
public class Shadow : MonoBehaviour {
bool sh;
public ParticleSystem particlesystem;
CharacterMotor cha = GetComponent<CharacterMotor>();
cha.movement.maxForwardSpeed = 30;// I need help HERE!
// Use this for initialization
void Start () {
sh=false;
particlesystem.enableEmission = false;
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.Q)) {
if (sh) {
sh=false;
particlesystem.enableEmission = false;
gameObject.GetComponent("Character Motor").movement.maxForwardSpeed = 15;//This is bad
gameObject.GetComponent("Character Motor").CharacterMotorJumping.baseHeight = 1;//This is bad
} else {
Vector3 fwd = particlesystem.gameObject.transform.position - transform.position;
Vector3 pos = new Vector3(transform.position.x,transform.position.y,transform.position.z);
RaycastHit hit;
if (Physics.Raycast(pos, fwd,out hit, 100)){
sh=true;
particlesystem.enableEmission = true;
//Debug.Log(hit.collider.gameObject.name);
gameObject.GetComponent("Character Motor").movement.maxForwardSpeed = 30;//This is bad
gameObject.GetComponent("Character Motor").CharacterMotorJumping.baseHeight = 5;//This is bad
}
}
}
}
}
Unity cannot assign 30 to maxForwardSpeed, it says:
Unexpected symbol `=' in class, struct, or interface member declaration
You can't call a method such as GetComponent on the main thread like that. $$anonymous$$ove the line where you get the component to the Start method and then assign the forward speed there.
Yes, you can use GetComponent like that (though it's not considered 'efficient', and it is also preferred to use generics). The problem is most likely accessing a JS script from a C# script. This is usually overcome by having the scripts you want to compile first in a 'special' folder :
Thanks a lot. I actually figured it out on my own, however I really appreciate your help, guys. $$anonymous$$eep up the good work!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
Can't remember how list.addrange goes 1 Answer
Automatically load asset bundles on start from cache 0 Answers