- Home /
Quick error in my code?
For some reason, it keeps telling me to add a semicolon at line (3,8) and (4,8) Referring to these two lines?
Line 3
Vector3 currentvertical = transform.TransformDirection(transform.up*Input.GetAxis("Vertical")*turnSpeed);
Line 4
Vector3 currenthorizontal = transform.TransformDirection(transform.right*Input.GetAxis("Horizontal")*turnSpeed);
Help would be great.
Full code:
var speed = 0.8;
var turnSpeed = 0.15;
var currentvertical : Vector3 = transform.TransformDirection(transform.up*Input.GetAxis("Vertical")*turnSpeed);
var currenthorizontal : Vector3 = transform.TransformDirection(transform.right*Input.GetAxis("Horizontal")*turnSpeed);
function Start () {
rigidbody.useGravity = false;
}
function Update () {
if (Input.GetButton("Jump")) { //Spacebar by default will make it move forward
rigidbody.AddRelativeForce (Vector3.forward*speed);
}
rigidbody.AddRelativeTorque(currenthorizontal);
// W key or the up arrow to turn upwards, S or the down arrow to turn downwards.
rigidbody.AddRelativeTorque(currentvertical);
// A or left arrow to turn left, D or right arrow to turn right.
}
$$anonymous$$ost likely when the compiler wants a semicolon at a place where it doesn't make sence you have something wrong in front of that line. The compiler reads your code like a book, it starts in line one and ends in the last one. If there's something wrong it can only tell at the point where your code stop making sence to him. It can't detect that you forget a closing bracket or something like that...
The whole script or at least a few lines above the error would help...
Feel free to edit your question at any time.
Answer by CHPedersen · Oct 12, 2011 at 07:10 AM
jahroy's final comment above is the correct one. :P
The reason you're getting that error is that you're trying to access transform outside a function. You can't do that, because transform is not a static variable, so it requires an object instance before you can access it. An object instance always exists inside a non-static function, such as Start.
Try moving those two lines into your Start-function and see if that doesn't solve the compiler error.
He should actually move the two lines into Update otherwise it doesn't make much sense. And of course remove the TransformDirection completely.
Ah. Duly noted. I wasn't considering what those two lines actually did, only why the compiler didn't accept them. :)
Answer by jahroy · Oct 12, 2011 at 12:43 AM
The only thing I can think of is that you are using javascript... so your variable declarations are incorrect (they use C# syntax).
In javascript you want to use:
var currentVertical : Vector3 = transform.TransformDirection(someVector);
var currentvertical : Vector3 = transform.TransformDirection(transform.up*Input.GetAxis("Vertical")*turnSpeed);
var currenthorizontal : Vector3 = transform.TransformDirection(transform.right*Input.GetAxis("Horizontal")*turnSpeed);
Changed, but now it says:
ArgumentException: You are not allowed to call get_transform when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function. $$anonymous$$ovement..ctor () (at Assets/New Folder/$$anonymous$$ovement.js:3)
I've never used Transform.TransformDirection, but from the documentation it sounds like it's used to transform a vector that is relative to an object into world coordinates.
If that's the case, it doesn't make much sense to scale the parameter you pass to TransformDirection.
It seems to me like you simply need to do this:
var worldVertical : Vector3 = transform.TransformDirection(transform.up);
var scaledVertical : Vector3 = currentVertical * someValue;
All that being said, it's really hard to tell what's going on when you only show one line of code.
I would say move the two lines in question inside the Start() function and enjoy...
The compiler is telling you that you can't call those functions when you're initializing class variables.
This is probably because there is no information about the associated transform until the game begins.
@jahroy: Transform.TransformDirection just applies the rotation (and scale i guess) of the transform, but not the translation. So the actual length of the vector stays the same (ok, it might be scaled but not shifted). Only the direction is changed. The direction is NOT normalized.
TransformPoint however will multiply the vector with the whole matrix of that transform and will also translate the vector by the position of the Transform.
This is usually controlled by the "w" component of the 4d-representation of the vector. If w is 1.0 the vector represents a position in local space and should be translated into world space. If w is 0.0 only the rotation is applied since the position vector inside the matrix gets multiplied by 0.0.
btw. transform.up equals transform.TransformDirection(Vector3.up);
It makes no sense at all to pass transform.up into TransformDirection since it's already in world space.