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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Kai_Gil · May 15, 2013 at 12:24 PM · movementrotate

Calling functions in Javascript

Here I have a script that creates two movement states for a game object - rotating or traveling in a straight line. However I want to create two functions in my script to call upon in the Update function, but I don't know how to do it. Here's what I've got:

 var pip : Transform;
 var gel : Transform;
 var nearPip = false; 
 var speed : float = -20;
 
 function Update () 
 {
     if(Input.GetButton("Fire1"))
     {
         if(nearPip == true)
         {
             var pivot = pip.position;
             gel.transform.RotateAround (pivot, Vector3.forward, 50 * Time.deltaTime);
         }
         else if (nearPip == false)
         {
             transform.Translate(Vector3(0,speed,0) * Time.deltaTime);
         }
     }
 }


But I'd much prefer to have it like this:

 if(nearPip == true){
 //insert Rotator function
 }
 
 else if(nearPip == false){
 //insert Movement function
 }
 
 
 //and then under the Update function...
 
 function Rotator(){
 var pivot = pip.position;
             gel.transform.RotateAround (pivot, Vector3.forward, 50 * Time.deltaTime);
 }
 
 function Movement(){
 transform.Translate(Vector3(0,speed,0) * Time.deltaTime);
 }


Any help on how to call these functions correctly would be great, thank you peeps!

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

3 Replies

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

Answer by AlucardJay · May 15, 2013 at 01:25 PM

You are so incredibly close.

To call a function, simply call it by name as you do with a command

 function PickMe()
 {
     // do stuff
 }

call the function with :

 PickMe();

note the parenthesis and the semicolon ();


So for your example script it would be :

 var pip : Transform;
 var gel : Transform;
 var nearPip = false;
 var speed : float = -20;
  
 function Update ()
 {
     if (Input.GetButton("Fire1"))
     {
         if ( nearPip == true )
         {
             //insert Rotator function
             Rotator();
         }
         else if ( nearPip == false )
         {
             //insert Movement function
             Movement();
         }
     }
 }
  
 function Rotator()
 {
     var pivot = pip.position;
     gel.transform.RotateAround (pivot, Vector3.forward, 50 * Time.deltaTime);
 }
  
 function Movement()
 {
     transform.Translate(Vector3(0,speed,0) * Time.deltaTime);
 }


Something else : it is worthy to note that :

 if ( nearPip == true )

is the same as writing :

 if ( nearPip )

and for not true :

 if ( nearPip == false )

is the same as writing :

 if ( !nearPip )

note the exclamation mark.

         if ( nearPip )
         {
             //insert Rotator function
             Rotator();
         }
         else if ( !nearPip )
         {
             //insert Movement function
             Movement();
         }
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 Kai_Gil · May 16, 2013 at 02:15 AM 0
Share

@alucardj this is exactly what I was after for and a nice explanation, thank you! :)

avatar image louismanuel · Feb 17, 2014 at 07:56 AM 0
Share

hello, that's a very nice formulated and documented answer thx a lot!!!

avatar image
0

Answer by programmrzinc · May 15, 2013 at 01:20 PM

You can call these functions easy!

        if(nearPip == true)
        {
          Rotator();
 
        }
        if(nearPip == false)
        {
          Movement();
 
        }
Comment
Add comment · 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
0

Answer by Schimauscha · Nov 07, 2014 at 09:13 AM

@alucardj Thank you very much! :)

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

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

18 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 avatar image avatar image avatar image

Related Questions

Updating rotation of client not working - strange error 0 Answers

Help with Grid Movement 2 Answers

Navigating the 3D platform 0 Answers

Rotate Character with movement 3 Answers

Orbit around an object 0 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