- Home /
JavaScript not working
Hi, me and a friend are making a game for a school project, and we've run into some difficulties. We've tried scripting the character movement on our own and none of our scripts seem to work. We've also tried many other scripts by other people and none of them work either. Here's an example of one of our scripts:
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
Have you attached the script to the game object in the Unity editor?
First, you should not put the reference to CharacterController in Update as it will have to figure that every frame. Is your terain a standard Unity one? If you are doing something special with the terrain then isGrounded might not ever be true.
If this was C# you would have a bunch of error about float/doubles; don't know if it matters for JS but add an "f" to your numbers at the top like 6.0f, 8.0f, etc
And as Nanobrain suggests, you have this script attached to your Player object with also has the CharacterController component attached/enabled at the same level in its hierarchy? I would expect you'd get NullRef errors if that wasn't the case but really don't know JS...
UnityScript/JS doesn't care if you put an "f" at the end of float values.
Is it so hard to describe what "doesn't work"? Do you get any errors at runtime or even at compile-time? Are you sure you don't set the speed variable in the inspector to 0 or something? Are you sure your "Horizontal" and "Vertical" axis are defined in the input manager?
Without a proper analysis and description of your problem you can't expect a solution from our side. Have you tried to Debug.Log some of the values inside Update?
Your answer
![](https://koobas.hobune.stream/wayback/20220613140711im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I'm trying to get an object to move using javascript however it won't move, can someone help? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Alternative to using transform.translate and transform.position for moving objects exact values? 1 Answer
Cant Get Character To Move 1 Answer