- Home /
Unable to find problems with 2d-scrolling movement code.
So I have been trying today to learn both a bit of coding and have to make my own game. I have been following this guys tutorials: http://www.youtube.com/user/TDCnoho?feature=watch but I am now stock on Episode 6, where I am making a sidescroller, and learning how to make the movement.
I tried to rip off the script directly from his video which can be found at 4:00, and what I have is:
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) {
// We are grounded. so recalculate
// move direction directly from axes
moveDirection = Vector3(0,0,Input.GetAxis('Horizontal*'));
//moveDirection = transform.TransformDirection(moveDirection);
//controller.transform.LookAt();
moveDirection *= speed;
if (moveDirection.sqrMagnitude > 0.01)
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(moveDirection),1);
- if(Input.GetButton ('Jump')) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity*Time.deltaTime;
// Move the controller
controller.move(moveDirection*Time.deltaTime);
I keep getting errors though,which are as following:
Assets/Platformer Movement.js(7,12): BCE0044: expecting (, found 'Update'.
Assets/Platformer Movement.js(7,20): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Platformer Movement.js(9,35): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Platformer Movement.js(21,17): BCE0043: Unexpected token: if.
Assets/Platformer Movement.js(21,19): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Platformer Movement.js(21,45): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Platformer Movement.js(30,1): BCE0044: expecting }, found ''.
All of these errors, and I have gone in and tried to alter it several times, but I cannot find the problem, or the difference in his and mine.
I just copied everything from the video, and that was how it looked. Changing it seems to do little in relation to the errors I am getting.
Answer by Linus · Aug 15, 2013 at 03:55 PM
function Update(){; should not have ; at the end
var controller : CharacterController = Getcomponent(CharacterController) should have ;
You are missing a } at the very end
Answer by robertbu · Aug 15, 2013 at 04:01 PM
In addition to the problems that @Linus lists, pay close attention to the case of letters.
'move' is not 'Move'
'Getcomponent' is not 'GetComponent'
'If' is not 'if'
In the console window, the first error is generally accurate (though the actual error is often on the preceding line). So the way to fix syntax errors is to click on the topmost error in the console window, Go to Mono and fix it, then go back to Unity and select the new topmost error, go back to Mono and fix the next error... Here is your script with all the syntax errors fixed:
#pragma strict
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) {
// We are grounded. so recalculate
// move direction directly from axes
moveDirection = Vector3(0,0,Input.GetAxis('Horizontal*'));
//moveDirection = transform.TransformDirection(moveDirection);
//controller.transform.LookAt();
moveDirection *= speed;
if (moveDirection.sqrMagnitude > 0.01)
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(moveDirection),1);
if(Input.GetButton ('Jump')) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity*Time.deltaTime;
// Move the controller
controller.Move(moveDirection*Time.deltaTime);
}
I just copied everything from the video, and that was how it looked. Changing it seems to do little in relation to the errors I am getting.
I seriously doubt your script is the way it looked in the video. Compare the fixed one above to the video.
Hey, I have tried adding this, and it doesn't give me the coding errors anymore, but now I get this error:
'NullReferenceException: Object reference not set to an instance of an object Platformer $$anonymous$$ovement.Update () (at Assets/Platformer $$anonymous$$ovement.js:9)'
This code expects a character controller. Select the object this script is attached to, then:
Component > Physics > Character Controller
UnityException: Input Axis Horizontal* is not setup. To change the input settings use: Edit -> Project Settings -> Input Platformer $$anonymous$$ovement.Update () (at Assets/Platformer $$anonymous$$ovement.js:12)
And now I get this. haha, im terrible at this.
Thank you so much for your patience and your helpfulness.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do you script in Java? Anyone have some exaple codes I could use? 0 Answers
I'm having troubles using a Capsule Cast 2 Answers
Programming in Unity 2 Answers