- Home /
Constant Running on x axis
Hi all,
I am new to Unity and I am trying to create a game (side scroller) where a character is constantly running on the x axis. The only controls I need is a jump function so the character can jump over obstacles. After searching and searching I found this code but I can't make any since of it and keep getting the following errors:
Assets/characterscripts/constantrun.js(1,29): BCE0043: Unexpected token: :. Assets/characterscripts/constantrun.js(1,44): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/characterscripts/constantrun.js(2,11): BCE0044: expecting :, found 'float'.
Code Follows:
public class CharacterMotor : MonoBehaviour
{
const float constantMotion = 10;
//If it really is a constant speed you can declare it as such and it will prevent you from changing it.
//Otherwise just remove the modifier.
public int moveSpeed;
public int jumpSpeed;
//how fast do we move side to side and jump.
public float gravity = 9.8f;
private CharacterController controller;
public void Start () {
controller = GetComponent<CharacterController>();
//cache a link to the Character Controller.
}
public void Update () {
Vector3 moveDirection = new Vector3( 0 , 0 , constantMotion );
//Create a new Direction vector
if( controller.isGrounded() ) { //Are we touching the ground?
moveDirection.x = Input.GetAxis("Horizontal") * moveSpeed;
if( Input.GetButtonDown("Jump") ) {
moveDirection.y = jumpSpeed;
//Set the vertical direction to the jump speed.
//moveDirection = transform.TransformDirection(moveDirection);
//You don't need this line since you will only be moving in one direction,
//But you will if your character rotates any.
}
}
moveDirection.y -= gravity * Time.deltaTime;
//This will pull the character back down.
controller.Move(moveDirection * Time.deltaTime);
//Move the character in the direction of moveDirection at a FPS independent rate.
}
}
Any kind of help will be greatly appreciated,
Thanks!
Ehm, dude... you posted the Character$$anonymous$$otor script which comes with Unity and is written in C#.
Your error is in line 29 of a UnityScript (JavaScript) called "constantrun.js". The posted code has nothing to do with your problem.
You should edit your question and post the correct code....
Well, actually the posted script looks like a modified version of the Character$$anonymous$$otor script. Is it possible that you copied this code into the "constantrun.js" file? That's C# code, the file needs the extention ".cs" and the filename have to be equal to the classname. In this case "Character$$anonymous$$otor.cs"
yeah I didn't realize that the code was C#. I am trying to learn by doing hands on and seeing what the code does and modify it accordingly. After pasting this into a .cs file I get the following error:
"ssets/Character$$anonymous$$otor.cs(2,31): error CS0246: The type or namespace name `$$anonymous$$onoBehaviour' could not be found. Are you missing a using directive or an assembly reference?"
I am trying to learn scripting but as you can see I have more to learn.
Your answer
![](https://koobas.hobune.stream/wayback/20220612183316im_/https://answers.unity.com/themes/thub/images/avi.jpg)