• 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 Jason87 · May 20, 2013 at 10:35 PM · gridgridmove

Re-enabling Grid Move script after re-selection

Hello everyone!

I am having an issue re-enabling the grid move script after i have deselected and then re-selected the same object. Step by step, this is what happens

  1. I select the object and my Grid Move script is enabled, which fires a while loop waiting for input from the player

  2. I deselect the object and I can see via the console log that the second while loop stops, which is exactly what i want

  3. I re-select the object, but the while loops do not fire back up again. I have the conditions of the while loop set as while(this.enabled && mp>0) Another problem i have is that it is subtracting one movement point (mp) when i deselect, even if i do not hit an arrow key, which i am guessing is happening because the entire while loop will fire, which ends in a mp-- command that will fire regardless.

here is my code:

 var walkSpeed : float = 1.0;
 var runSpeed : float = 2.0;
 var gridSize : int = 1;
 enum Orientation {Horizontal, Vertical}
 var gridOrientation = Orientation.Horizontal;
 var allowDiagonals = false;
 var correctDiagonalSpeed = true;
 private var input = Vector2.zero;
 var character: Character;
 var selected: boolean = false;//can be deleted when debug done
 var mp: int;
     
 function Start ()
 {
      character = GetComponentInChildren(Character);
      mp = character.getMovementPoints();
      var myTransform = transform;
      var startPosition : Vector3;
      var endPosition : Vector3;
      var t : float;
      var tx : float;
      var moveSpeed = walkSpeed;
      //if(this.enabled)
      //{
           while (this.enabled && mp > 0)
           { 
                Debug.Log("first while loop passes");
                while (this.enabled && input == Vector2.zero)
                {
                     Debug.Log("second while loop passes");
                     GetAxes();
                     tx = 0.0;
                     yield;
                }
                transform.forward = Vector3.Normalize(new 
                Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")));
                startPosition = myTransform.position;
                endPosition = gridOrientation == Orientation.Horizontal?
                Vector3(Mathf.Round(myTransform.position.x), 0.0, 
                Mathf.Round(myTransform.position.z)) +
                Vector3(System.Math.Sign(input.x)*gridSize, 0.0, 
                System.Math.Sign(input.y)*gridSize)
                :
                Vector3(Mathf.Round(myTransform.position.x), 
                Mathf.Round(myTransform.position.y), 0.0) +
                Vector3(System.Math.Sign(input.x)*gridSize, System.Math.Sign(input.y)      *gridSize, 0.0);
                t = tx;
                while (t < 1.0)
                {
                     Debug.Log("third while loop passes");
                   //moveSpeed = Input.GetButton("Run")? runSpeed : walkSpeed;
                     t += Time.deltaTime * (moveSpeed/gridSize) * 
                     (correctDiagonalSpeed && input.x != 0.0 && input.y != 0.0? .7071 : 1.0);
                     myTransform.position = Vector3.Lerp(startPosition, 
                     endPosition, t);
                     yield;
                }
                tx = t - 1.0; // Used to prevent slight visual hiccups on "grid lines" due to Time.deltaTime variance
                GetAxes();
              //added to subtract one movement from moved character
                yield;
           }
      //}
 mp--;
 } 
 
 function Update()
 {
     
 }
     
 function GetAxes ()
 {
      if(this.enabled)
      {
           input = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
           if (allowDiagonals)
           return;
           if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
           {
                input.y = 0.0;
           }
           else
           {
                input.x = 0.0;
           }
      }
 }


While typing this out i realized it could be because there is no update function present, its all in a start function which i know is best for while loops. And im questioning if this entire deal would be solved if i stuck if(this.enabled) in the update function, but what would i call underneath? Can you call Start()?

Also, that will not fix my movement points issue (mp).

Any thoughts on how to fix that issue, and if there is a more efficient way of doing this I would love to know. I have racked my brain on this for a day and I'm fairly positive i just need to stick an if statement in update checking if enabled, but again not sure if i can call start from that.

Thanks for any help and advice you guys can give me! :-)

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

13 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

Related Questions

Moving the cube in a grid cell by cell 0 Answers

Tell an object that a position in grid is occupied 0 Answers

How to detect collision of a wall with grid movement? 1 Answer

Why the player is keep moving sometimes out of the grid area ? 0 Answers

how to make objects move in rows and columns? 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