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 alberto-lara · Jun 16, 2013 at 03:15 AM · cameratransformmaintransformdirection

I got "Your not allowed to call this function..." in this code:

Hello, I got the error "UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function. controlBola..ctor () (at Assets/controlBola.js:10)" in this code:

 #pragma strict
 //Fuerza aplicada al mover la bola:
 var fuerza:float=5;
 //Banderas de movimiento:
 var izq:boolean=false;
 var der:boolean=false;
 var arr:boolean=false;
 var aba:boolean=false;
 //Referencia para la camara:
 var camTransform : Transform=Camera.main.transform;
 //Direccion relativa de movimiento (depende de la camara):
 var izqRelativo:Vector3=camTransform.TransformDirection(Vector3.left);
 var derRelativo:Vector3=camTransform.TransformDirection(Vector3.right);
 var delRelativo:Vector3=camTransform.TransformDirection(Vector3.forward);
 var detRelativo:Vector3=camTransform.TransformDirection(-Vector3.forward);
 function FixedUpdate () {
     verificarTeclasPresionadas();
     verificarTeclasLiberadas();
     aplicarFuerzas();
 }
 function verificarTeclasPresionadas()
 {
     if(Input.GetKeyDown("a")||Input.GetKeyDown("left")) izq=true;
     if(Input.GetKeyDown("d")||Input.GetKeyDown("right")) der=true;
     if(Input.GetKeyDown("w")||Input.GetKeyDown("up")) arr=true;
     if(Input.GetKeyDown("s")||Input.GetKeyDown("down")) aba=true;
 }
 function verificarTeclasLiberadas()
 {
     if(Input.GetKeyUp("a")||Input.GetKeyUp("left")) izq=false;
     if(Input.GetKeyUp("d")||Input.GetKeyUp("right")) der=false;
     if(Input.GetKeyUp("w")||Input.GetKeyUp("up")) arr=false;
     if(Input.GetKeyUp("s")||Input.GetKeyUp("down")) aba=false;
 }
 function aplicarFuerzas()
 {
     if(izq)  rigidbody.AddForce(izqRelativo*fuerza);
     if(der) rigidbody.AddForce(derRelativo*fuerza);
     if(arr)  rigidbody.AddForce(delRelativo*fuerza);
     if(aba) rigidbody.AddForce(detRelativo*fuerza);
 }



I really don´t know why is this because I checked the example on the offical documentation and it's this:

 // Calculate the x-axis relative to the camera
 var cam : Transform = Camera.main.transform;
 var cameraRelativeRight : Vector3 = cam.TransformDirection (Vector3.right);
 // Apply a force relative to the camera's x-axis
 rigidbody.AddForce (cameraRelativeRight * 10);


Many thanks.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Jun 16, 2013 at 03:46 AM

You must not run any code outside functions. Only declare variables outside functions. The code example in the documentation is assumed to be inside a function. If you want camTransform as a global variable, then declare it outside any functions:

 var camTransform : Transform;

Then run code inside a function:

 function Start () {
     camTransform = Camera.main.transform;
 }

Also, do not use FixedUpdate for anything except physics. Only use Update for checking input; it will not work right in FixedUpdate since you will miss KeyDown/KeyUp events. It's better to use enums (KeyCode.A) instead of strings ("a") for input functions. Although you'd often be better off with GetButton instead of hard-coding input keys, since that won't work on all keyboards (WASD is no good on AZERTY keyboards, for example).

Comment
Add comment · Show 1 · 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 alberto-lara · Sep 23, 2013 at 05:15 PM 0
Share

Thank you very much :D that works.

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

15 People are following this question.

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

Related Questions

How do i lock the position of the camera above the player relative to the origin point? 0 Answers

Change position of camera on scene load? 1 Answer

How can i make my crosshair drag behind the camera? 0 Answers

Simple Camera Movement?! (javascript) 1 Answer

When switching camera position... 1 Answer


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