- Home /
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"); }
}
}
}
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!");
}
}
}
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?
@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.
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.
Your answer
Follow this Question
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