- Home /
'else' not working.... please help:(
the script I have is not working when I'm pretty sure it should be but when I try to play it, it says Assets/SCRIPTS/character script.js(20,1): BCE0044: expecting }, found 'else'. someone please help, its very frustrating while still trying to learn coding..
#pragma strict
var rotationSpeed : float = 10;
var walkSpeed : float = 7;
var gravity : float = 50;
private var yRot : float;
function Update () {
var Controller : CharacterController = GetComponent(CharacterController);
var vertical : Vector3 = transform.TransformDirection(Vector3.forward);
var horizontal : Vector3 = transform.TransformDirection(Vector3.right);
if
(Input.GetAxis("Vertical")) (Input.GetAxis("Horizontal"));
{
animation.CrossFade("Run", 0.2);
Controller.Move((vertical * (walkSpeed * Input.GetAxis("Vertical"))) * Time.deltaTime);
Controller.Move((horizontal * (walkSpeed * Input.GetAxis("Horizontal"))) * Time.deltaTime);
}
else
{
animation.CrossFade("Idle", 0.2);
}
if
(Input.GetAxis("Mouse X")){
yRot += 10 * Input.GetAxis("Mouse X");
}
{
transform.rotation = Quaternion.Euler(0, yRot, 0);
}
the 'else' is on line 20...
Answer by perchik · Jul 02, 2014 at 04:59 PM
Update:
your code is just flat out wrong. What are you trying to do? Input.GetAxis()
returns a float between -1 and 1. So the comparison I wrote below fails because you have to compare floats to something.
Perhaps:
if( Input.GetAxis("Vertical") >.5f && (Input.GetAxis("Horizontal") >0 )
but that may not do what you want.
old post
Everyone who answered (including me) this made typos.
This is your problem:
if
(Input.GetAxis("Vertical")) (Input.GetAxis("Horizontal"));
You have a semicolon at the end, but when you get rid of it, it's going to throw more errors.
Removing the semicolon:
if (Input.GetAxis("Vertical")) (Input.GetAxis("Horizontal"))
Then, @insominx is correct, that's still bad syntax (but their answer has an extra paren too). If you mean to check if one OR the other, do this:
if (Input.GetAxis("Vertical") || (Input.GetAxis("Horizontal") )
if you want to check to see if BOTH of them:
if( Input.GetAxis("Vertical") && (Input.GetAxis("Horizontal") )
Notice the parentheses, Input.GetAxis() has one set of parens, the if statement has two.
You have pointed out the obvious syntax errors, but this statement should still fail to compile as there is no comparison happening here. Those calls to Input.GetAxis are returning floats.
@RovenGamer needs to figure out what should be compared within that if statement, while learning the proper syntax for if statements
it still doesnt recognize the || or the &&.. it says Assets/SCRIPTS/character script.js(13,32): BCE0043: Unexpected token: &&.
You didn't even get as far as the rather cryptic lines at the end... :) Basically the whole lot is nonsense.
if
(Input.GetAxis("$$anonymous$$ouse X")){
yRot += 10 * Input.GetAxis("$$anonymous$$ouse X");
}
{
transform.rotation = Quaternion.Euler(0, yRot, 0);
}
Answer by GameVortex · Jul 02, 2014 at 03:43 PM
On line 14 you have ended your If statement with a semicolon. The else therefore have no If to belong to. Remove the semicolon and you are good to go.
Nope :( now its also saying "Assets/SCRIPTS/character script.js(14,66): UCE0001: ';' expected. Insert a semicolon at the end."
Answer by insominx · Jul 02, 2014 at 04:11 PM
Line 13 and 14 look pretty odd:
if (Input.GetAxis("Vertical")) (Input.GetAxis("Horizontal"));
Maybe you want somethign more like this:
if (Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")
{
....
}
ive tried that... it says "Assets/SCRIPTS/character script.js(14,37): BCE0043: Unexpected token: ||."
Answer by Kiwasi · Jul 02, 2014 at 07:49 PM
As pointed out the code is full of holes. Here is a version that shouldn't throw errors, won't guarantee I've caught everything.
#pragma strict
var rotationSpeed : float = 10;
var walkSpeed : float = 7;
5.var gravity : float = 50;
private var yRot : float;
function Update () {
var Controller : CharacterController = GetComponent(CharacterController);
var vertical : Vector3 = transform.TransformDirection(Vector3.forward);
var horizontal : Vector3 = transform.TransformDirection(Vector3.right);
if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) {
animation.CrossFade("Run", 0.2);
Controller.Move((vertical * (walkSpeed * Input.GetAxis("Vertical"))) * Time.deltaTime);
Controller.Move((horizontal * (walkSpeed * Input.GetAxis("Horizontal"))) * Time.deltaTime);
} else {
animation.CrossFade("Idle", 0.2);
}
if (Input.GetAxis("Mouse X") != 0){
yRot += 10 * Input.GetAxis("Mouse X");
transform.rotation = Quaternion.Euler(0, yRot, 0);
}
}
My suggestion is to go to the beginner tutorials. You have a desperate need to understand the basics of scripting
thank you so much man. and yeah ive been trying to learn how from a bunch of youtube tutorials but im still confused on most of the basics
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why am i getting this error?! 2 Answers
error CS1525, please help 0 Answers
Errors when trying to play my project 3 Answers
please help island demo compieler error 2 Answers