How do i make an object render in front of another for a lock on target

As the title says im trying to render an object in front of others, i maneged to do it but i dont know how i could make the rendered object stay in a specified location, see images

what happens:


what i would like to achieve:

hierarchy:

So yeah the object renders in front of others, but not in a specified location obviusly

sorry for bad title, i dont know how to describe it

Three (3) primary ways that Unity draws / stacks / sorts / layers / overlays stuff:

In short,

  1. The default 3D Renderers draw stuff according to Z depth - distance from camera.

  2. SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties

  3. UI Canvas Renderers draw in linear transform sequence, like a stack of papers

If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:

  • identify what you are using
  • search online for the combination of things you are doing and how to to achieve what you want.

There may be more than one solution to try.

For instance, you may even use multiple co-located cameras to more-explicitly control apparent draw ordering.

Additional reading in the official docs:

And SortingGroups can also be extremely helpful in certain circumstances:

yeah but i dont have a problem rendering the object, i have a problem positioning the rendered object in a specific way, and it not even a problem its just me who cant do it.
I though that there could be two camera, they’re the same, but one renders only one thing(the target icon), so like they function the same but one renders only one layer

Okay i made it better, now it stays in place but it still moves around a little bit depending on how i move, i have to fix it but idk how

Is it perhaps an order of operation problem?

Generally if you need a crosshair to track something, the something has to move FIRST, so that when the crosshair tries to go to it, it is correct.

Since you’ve provided zero code all I can suggest is to explicitly update your 3D object, THEN update your crosshairs.

what do you mean by updating the 3d object?anyway i posted no code because there is no code for this thing.
The only piece of code regarding the lock object is this:

aimIcon.transform.position = new Vector3(targetPos.x, targetPos.y + 0f, targetPos.z);
All it does is transforming the object location above the currentTarget location( so above target’s head)

Updating either the thing you’re looking at (the target) or moving the camera such that it appears to move.

Think about it as data flows over the duration of each single frame:

orientation of target + orientation of camera → desired location of crosshairs

This means DON’T set the crosshairs until you have addressed ALL sources of movement, eg, moving the target or moving the camera.

Thanks for the help you made me realize this, I notice that the more the camera is distant the closer to the ground the target will be, so now i’m gonna make a script that move the camera up and down based on the distance between the target and the camera