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 NDPW · Mar 13, 2015 at 03:41 PM · javascriptupdatewhile

Using while statements

Hello guys,

I am facing a little issue at the moment, I am trying to make a player control script witch makes the player move to the touched position, unfortunately the while statement doesn't work when placed within the update function (unity freezes). I was wondering if somebody here could help me with this problem, this is the script I am using right now:

 #pragma strict
 
 var pos : Vector2;
 var playerposx = GameObject.Find("player2").transform.position.x;
 var playerposy = GameObject.Find("player2").transform.position.y;
 static var speed : float = 10;
 
 function Start () {
 
 }
  
 function Update() {
      pos = Camera.main.ScreenToWorldPoint(Vector2(Input.mousePosition.x, Input.mousePosition.y));
     
     if (Input.mousePosition.x > (Screen.width/2)) {
          
          while (Input.mousePosition.y > playerposy)
         {
             rigidbody2D.velocity.y = speed;
         }
     
         while (Input.mousePosition.y < playerposy)
         {
             rigidbody2D.velocity.y = speed * -1;
         }
     
         while (Input.mousePosition.x < playerposx)
         {
             rigidbody2D.velocity.x = speed * -1;
         }
     
         while (Input.mousePosition.x > playerposx)
         {
             rigidbody2D.velocity.x = speed;
         }
     
      }
 }


The question is, how do I make this script work?

Comment
Add comment · Show 17
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 Bonfire-Boy · Mar 13, 2015 at 03:48 PM 1
Share

I think the problem is that the while statements ARE working. Each of those whiles will cause it to pause until the the condition isn't met. But at least one of them will be met whenever the mouse is somewhere other than (playerposx, playerposy), Replacing all the whiles with ifs is probably the answer.

avatar image gcoope · Mar 13, 2015 at 03:55 PM 1
Share
          if(Input.mousePosition.y > playerposy)
          {
              rigidbody2D.velocity.y = speed;
          }
      
          else if(Input.mousePosition.y < playerposy)
          {
              rigidbody2D.velocity.y = speed * -1;
          }
      
          else if(Input.mousePosition.x < playerposx)
          {
              rigidbody2D.velocity.x = speed * -1;
          }
      
          else if(Input.mousePosition.x > playerposx)
          {
              rigidbody2D.velocity.x = speed;
          }
avatar image Bonfire-Boy · Mar 13, 2015 at 04:12 PM 1
Share

@gcoope Two issues with that... First, you probably don't want movement in y direction to prevent movement in x direction, so the second else is unwanted. Second, the first and third elses are superfluous because a value cannot be simultaneously greater and smaller than another value (it may improve efficiency by bypassing a test you know is going fail, but it won't change the functionality).

avatar image NoseKills · Mar 13, 2015 at 04:15 PM 2
Share

If yo write it like that it surely will. Now it's only moving the x coordinate if the y coordinate matches with the mouse position.

You need to remove at least the 2nd "else"

           // move on y axis if not at cursor position
           if(Input.mousePosition.y > playerposy)
           {
               rigidbody2D.velocity.y = speed;
           }
       
           else if(Input.mousePosition.y < playerposy)
           {
               rigidbody2D.velocity.y = speed * -1;
           }
           // move on x axis if not at cursor position
           if(Input.mousePosition.x < playerposx)
           {
               rigidbody2D.velocity.x = speed * -1;
           }
       
           else if(Input.mousePosition.x > playerposx)
           {
               rigidbody2D.velocity.x = speed;
           }


It should make sense if you just "read it aloud"

If cursor is higher than player, move up. If not (else), and if cursor is below player move down...

avatar image Bonfire-Boy · Mar 13, 2015 at 04:16 PM 1
Share

@NDPW Can you explain further? Satisfying one of the ifs shouldn't stop it checking subsequent ones (unless you use else if as in @gcoope's comment). Try simply replacing the every while with if (or you can use else if but only to bypass tests you know are going to fail... in other words where @Nose$$anonymous$$ill's comment shows them)

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Variables in GUI not updating/changing? (javascript) 1 Answer

WaitForSeconds in Update() function 4 Answers

Getting updated variables from another script 2 Answers

Script is causing immense lag, and I don't know what's causing it. 2 Answers

A while type statement in the Update function, Yield? 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