Matching relative position

I asked a similar question a while ago, it worked at the time, but after a few changes, it suddenly no longer works.

Long story short, I have an object I want to move relative to another object, but far away.

Object 1: (0, 0, 0)
Object 2: (10, 10, 10)

Move object 1 to (5, 10, 15)
Object 2 should now be (15, 20, 25), but relative to object 1, still just (10, 10, 10).

I have an algorithm that worked previously, which was basically:

vector3 offset = object1.transform.position - object2.transform.position;
Object2.transform.position += offset;

And this worked in the past… dont know how though. Any help is appreciated. Maybe im just being stupid today.

Your English description sorta covers it, but your code isn’t doing that.

Line 1 above is likely correct, to execute ONCE, in order to observe the offset desired.

Then Line 2, which should be executed every frame, needs to take #1’s position and add offset, set it into #2’s position.

obj2.transform.position = obj1.transform.position + offset;

ALSO, don’t name things “object”… call it what it is: leader, follower, player, henchman, pet, etc.

Riiight I must have accidentally misread my own old code. I WAS being stupid.

Also I think line 1 has to run constantly, as its moving relative to the player, which is also moving.

(when I wrote this my code was closed, I forgot the name and just abbreviated to object 1 and 2 for simplicity). Ill test this out soon, thanks for the help!

OK so, it partially worked, but added some new issues. I can go into more detail this time at least.

Making a space themed game, and I like the real scale of space, but without the real SIZE of space (which is too large obviously), so what this does is allow planets (and similar) to move relative to the camera to create a “huge planet” effect.

The camera orbits the players main ship, which hops around between POIs, like the game FTL, but in 3D. Whenever you arrive, that planet inflates its scale from 0 to its default (to look like you are getting closer), and activates this camera algorithm. It takes the offset of the camera relative to the planet, which moves the planet relative to the camera, that way the planet seems massive and unmoving (no matter how fast your ship moves), even though its only slightly larger than the player ship. Relative to the cameras movement, the planet doesnt move. Camera moves sideways, planet moves sideways too. Camera orbits ship in an arc, planet moves in that same arc.

When I used your updated code, it created that affect, but the planet would teleport back and forth between its intended location, and somewhere else, roughly mirrored behind the ship, making the effect work, but flickering between two places at once.

For clarity, the planet IS a child of the system generator, if that changes things.

AHA nevermind, I got it with a few modifications. You were right, i DONT have to update the offset, thats what caused the flicker. But if I kept everything else, the planet would teleport to the location thats mirrored. What it should be is:

//runs once to set the offset
offset = Camera.main.transform.position - location.transform.position; //location is the planet you are at

//runs constantly to create the effect
location.transform.position = Camera.main.transform.position - offset;

You have to SUBTRACT the offset, otherwise it creates a negative version of the location relative to the camera, at least thats what I got from it.

Thanks for the help, I was in fact stupid by settings the locations position equal to itself plus the offset, haha!

ezgif-5f93bd281bdf99

For context, the planet is only slightly larger than the ship (its about 2-3 ships length away) but in game, it look infinitely large. You can tell its close because i set it to clip into the rings.

Yeah, that’s one of those things that no matter how many times you try to get it right the first time, you always have to do a quick check and print out the offset.

Since generally with an offset you want to ADD it (rather than subtract it), the way to get an “addable offset” is this:

AddableOffset = TargetPosition - MyPosition;

The memory trick is : I am here, what do I need to add to myself to get to target?

But yeah, I still get it wrong some fraction of the time. I’m always watching for it…