Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by blackmethod · Oct 11, 2011 at 11:55 PM · errorvector3semicolon

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. 
 
     }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Oct 12, 2011 at 12:04 AM 0
Share

Just checking, it's a .cs file right?

avatar image Bunny83 · Oct 12, 2011 at 12:59 AM 0
Share

$$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.

avatar image blackmethod · Oct 12, 2011 at 05:13 AM 0
Share

Sorry, fixed.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 13, 2011 at 01:35 AM 0
Share

He should actually move the two lines into Update otherwise it doesn't make much sense. And of course remove the TransformDirection completely.

avatar image CHPedersen · Oct 13, 2011 at 06:50 AM 0
Share

Ah. Duly noted. I wasn't considering what those two lines actually did, only why the compiler didn't accept them. :)

avatar image
0

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);
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image blackmethod · Oct 12, 2011 at 12:50 AM 0
Share

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)

avatar image jahroy · Oct 12, 2011 at 02:32 AM 0
Share

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.

avatar image blackmethod · Oct 12, 2011 at 05:14 AM 0
Share

Sorry, added the full code at the top again.

avatar image jahroy · Oct 12, 2011 at 05:49 AM 1
Share

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.

avatar image Bunny83 · Oct 13, 2011 at 01:33 AM 0
Share

@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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot implicitly convert type 'int' to 'UnityEngine.Vector3' 3 Answers

';' expected. Insert a semicolon at the end. I already have a ; 1 Answer

Tansform.position assign attempt for 'Enemy(Clone)' is not valid. Input position is {NaN, NaN, NaN} 0 Answers

I keep getting: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(float, UnityEngine.Vector3, UnityEngine.Quaternion)' was found. 1 Answer

expected. Insert a semicolon at the end. When the end that it says, is a } Could someone help us fix this? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges