[noob] question about movement and location of another obj

Sorry for such a noob question, but I can’t seem to find it.

Anyways, I have an object and if another instance of the same object does not exist below it, the current object will translate 50 px down. What function is there to tell if there is an object that is 50px below the current one? I could use physics, but it would make things a little more complicated.

Would I have to use a collision function? please help, i’m not very good.

By ‘px’, do you mean pixels? Is this a 2-d game?

In any case, there are several ways you could determine if there’s an object below the current object. Here are a few:

  1. Attach colliders (and rigid bodies if necessary - can’t remember if that’s needed) to the object, and raycast downward from the object. If a hit is returned, check the distance to the object to see if it’s directly below the object (if that’s important).

  2. Acquire all of the objects of the specified type by name or tag (you might be able to cache this for performance), and iterate through them to see if any are below the current object.

  3. Using the physics system again, use the ‘sphere’ query to check nearby objects to see if any are below the current object.

And so on. (What solution will be optimal depends on various factors, such as how many objects are in the scene, whether they’re continually created and destroyed, etc.)

Yea is a 2D game and px represents pixels…

I thought of using colliders, but you have to be in collision of an object. How can do you collision without actually colliding?

Is it possible to give me an example of how to use the downward raycast? I would really appreciate it!

Using the built-in raycast function would require colliders. If you don’t want to use colliders though, you can do it manually.

Let’s assume for the moment that performance isn’t a concern and will not be a problem. The first step then is to acquire all of the objects of the type in question. You can use (for example) GameObject.FindGameObjectsWithTag() for this.

Once you have all of the game objects, loop through them (ignoring the one that’s the same as the one you’re checking below). I don’t know how your game is set up exactly, but any of the objects that are more or less below the test object should have an x value that is within a certain range of the test object’s x value, so that’s the first thing you’ll want to shoot for. If you find any such objects, the object of interest will be the first one with a y value that is lower than the test object’s y value, but higher than the y values of all the other objects with appropriate x values (or vice versa, depending on the coordinate system).

If this doesn’t make sense, try drawing some examples on paper. See how the coordinates of any objects below the test object would relate to the coordinates of the test object itself; all we’re trying to do here is formalize these observations (which are fairly trivial) and then express them in code.

If that’s not enough to get you going, try breaking it down into subproblems and tackling them one at a time. Then, if you run into a specific problem with a particular step, post back here and someone will most likely be able to help.