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 Fraeger · Nov 09, 2013 at 07:30 PM · javascript

Sequence of function Calls

Hi,

I am trying to get a cube to display some text if the player rightclicks on it and is close enough to it. If the player is not close enough, he will automatically walk towards it and display the text as soon as he is close enough. If the player chooses to walk somewhere else instead, the function should stop.

I thought I would have a a bool that gets set to false when the player clicks on the cube and gets set to true if he clicks somewhere else and the message would only display when that bool is set to false. So far so good. However, everytime I click on the cube, it sets the bool to false and then immediatly to true again.

So my question would be if I can somehow determine in which order those commands are being executed?

Also, I am completely new to this (literally started yesterday), so if there is a much better way to achieve this, please enlighten me :D

Here`s the code I have so far (the walking is handled in a different script that is working fine):

 #pragma strict
 
 var CubeObject :GameObject;
 var Player :GameObject;
 private var distance :float;
 private var RMBclicked :boolean;
 
 function OnMouseOver() 
 {
             
         if (Input.GetMouseButtonDown(1))
         {          
             RMBclicked = false;
             print ("false");
                   
          }
                
 }
 
 
 function Update ()
 {
     distance = Vector3.Distance(CubeObject.transform.position, Player.transform.position);
     
             
     if (Input.GetMouseButtonDown (1))
             {
                 RMBclicked = true;
                 print ("true");
             }      
             
     if (distance <3)
             {
                 if (RMBclicked == false)
                 {
                     print ("Hello");
                     //RMBclicked = true;
                     //print ("true Dist");
                     
                 }
             }  
 
 }
 
     
 function LateUpdate ()
 {
 
     
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cmpgk1024 · Nov 09, 2013 at 08:03 PM

You are setting RMBClicked to both true and false every time you click the right mouse button. Try using this:

 function Update ()
 {
     distance = Vector3.Distance(CubeObject.transform.position, Player.transform.position);
  
  
     if (Input.GetMouseButtonDown (1) && distance < 3)
          {
           print ("hello");
          } 
      else if(distance > 3){
            RMBclicked = true;
       }
 
    if(RMBclicked){
       //move player to cube
       RMBclicked = false;
     }
  
 }
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 Fraeger · Nov 09, 2013 at 10:31 PM

Hello! Thank you very much for the help, however there is still a little problem. when the player is too far from the cube, it will set the button to true and the player will walk there. But it will not check for distance and RMBclicked again, so it will not display the message.

However, I did fix it with the help of a friend by using getMouseButtonUp instead of GetMouseButtonDown (thereby creating a small delay, setting the bool first to false and then to true).

So this it what it looks like in case anybody is interested.

var CubeObject :GameObject; var Player :GameObject; private var distance :float; private var RMBclicked :boolean;

function OnMouseOver() {

     if (Input.GetMouseButtonUp(1))
     {          
         RMBclicked = false;
         print ("false");
               
      }
            

}

function Update () { distance = Vector3.Distance(CubeObject.transform.position, Player.transform.position);

 if (Input.GetMouseButtonDown (1))
         {
             print ("true");
             RMBclicked = true;
         }      
         
 if (distance <3)
         {
             if (RMBclicked == false)
             {
                 print ("Hello");
                 //RMBclicked = true;
                 //print ("true Dist");
                 RMBclicked = true;
                 
             }
         }  

}

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

17 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I single out one object amongst a group of objects with the same tag? 3 Answers

Animation plays on when colliding with a trigger 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