Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 glg · Dec 25, 2019 at 11:08 AM · animatorpositiontags

Tag object change position

Hello everyone. I'm struggling with a problem and I'm going crazy. I have a total of 500 objects (images), to which I have applied a "ToReset" tag to identify them via c # script. I would like that when one of these 500 objects changes its position (x / y) a gameobject present in the scene comes alive and when the object returns to its initial position (0,0,0) an object is applied second animation. I can't solve it.

Here is the code: { GameObject[] ImageToReset; Vector3 Pos; float Position = 0.0f; public Animator ButtonAnim;

     // Start is called before the first frame update
     void Start()
     {
         ImageToReset = GameObject.FindGameObjectsWithTag("ToReset");
         foreach (GameObject ToReset in ImageToReset)
         {
             Pos = ToReset.transform.localPosition;
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         foreach (GameObject ToReset in ImageToReset)
         {
             Vector3 offset = ToReset.transform.localPosition - Pos;
             if (offset.x > Position) { ButtonAnim.SetBool("Active", true); Debug.Log("attivo"); }
             if (offset.x == Position) { ButtonAnim.SetBool("Active", false); Debug.Log("disattivo"); }
             if (offset.y > Position) { ButtonAnim.SetBool("Active", true); Debug.Log("attivo"); }
             if (offset.y == Position) { ButtonAnim.SetBool("Active", false); Debug.Log("disattivo"); }
         }
     }
 }
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

Answer by bhavinbhai2707 · Dec 25, 2019 at 06:28 PM

I am not sure what exactly you needed but I am assuming that you simply want to check if any of 500 objects of yours have moved from its position? if yes then do something. if that is what you need then the reason it may not be working is because, you are storing initial positon to Pos(which is neither array or list). so, in that foreach loop in start is overriding all your previous position stored. so it won't be of any use later. also, I don't understand your use of subtracting pos from Image position in offset and don't use foreach loop alot. specially in a gameloop. it's very heavy in terms of use. I made changes to your script and tested it. It works for me. I hope this is what you need. feel free to ping if something else.

     public GameObject[] ImageToReset;                    //array of your Images
     public Vector3[] Pos;                                //Will store position of all Images
     void Start()
     {
         ImageToReset = GameObject.FindGameObjectsWithTag("ToReset");
         Pos = new Vector3[ImageToReset.Length];                                
         for (int i = 0; i < ImageToReset.Length; i++)
         {
             Pos[i] = ImageToReset[i].transform.position;
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         for(int i = 0; i < ImageToReset.Length; i++)
         {
 
             Vector3 offset = ImageToReset[i].transform.position;
             if (offset.x > Pos[i].x || offset.x < Pos[i].x)
             {
                 Debug.Log(ImageToReset[i].name + "Moved in X axis");
             }
             if (offset.y < Pos[i].y || offset.y > Pos[i].y)
             {
                 Debug.Log(ImageToReset[i].name + "Moved in Y axis");
             }
             if (offset.x == Pos[i].x && offset.y == Pos[i].y) 
             { 
                 Debug.Log(ImageToReset[i].name + " Did not move at all!"); 
             }   
         }
     }

Comment
Add comment · Show 12 · 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 glg · Dec 26, 2019 at 06:12 PM 0
Share

Thanks so much. Unfortunately it does not work as the images are subject to an entry animation. In practice, they are in the position (-100, -100.0) and reach the position (0.0.0) through the animation. At the end of the animation, after they have reached the position (0,0,0), if the image moves on the axes (x / y) the event should start. How can I solve it?

avatar image bhavinbhai2707 glg · Dec 27, 2019 at 07:44 AM 0
Share

@glg sorry I forgot that you were using Images. You always have to use RectTransform when using UI Components. In the above code just change all the ImageToReset[i].transform.position lines to ImageToReset[i].GetComponent<RectTransform>().transform.position. It will work.

avatar image glg bhavinbhai2707 · Dec 28, 2019 at 03:37 PM 0
Share

I changed the lines of code but it doesn't work. however, if I deactivate the animator, it seems to work only for some images.

Show more comments

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

163 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 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 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 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 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 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 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

2D Animation does not start 1 Answer

Camera rotation around player while following. 6 Answers

Animation position change 0 Answers

Accessing the different position of a game object with same tags 1 Answer

How to set Position and Rotation of character during a Read-only animation (in code perhaps?),Set Position and rotation during animation 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