How to move an object and keep its distance relative to the player?

Hi,

I’m trying to create a script that will keep a planet and a sun (both game objects) at a constant distance from the player space ship, so that the player can never reach them. I’ve tried searching everywhere for a similar script to work from but can’t seem to find anything, so I had a go myself. I’m still learning scripting though!

Here’s what I’ve done so far:

#pragma strict

var isActive : boolean = false;

private var objects : GameObject[];
private var modLoc : Vector3;
private var origPos : Vector3;
var objectLoc = new Array (Vector3(0, 0, 0));

function Start () {
 
 objects = GameObject.FindGameObjectsWithTag("keepDistance");

 for (var object in objects) {
 objectLoc.Push(object.transform.position);
 }
 origPos = this.transform.position;
 
}

function Update () {
 
 if (isActive) {
 modLoc = origPos - this.transform.position;
 //modLoc.z = 0;
 var i = 0;
 var modded : Vector3;
 for (var object : GameObject in objects) {
 modded = objectLoc *+ modLoc;*

object.transform.position = modded;
i++;
}
}

}
The idea of the script is to store the positions of all objects tagged ‘keepDistance’ in the objects array. The player’s starting position is stored in origPos.
Then, if the script is active, every Update modLoc stores the relative movement of the player and the script iterates through the stored objects array to add modLoc relative movement to them - the idea being that they will stay at a constant distance from the player as they move.
I seem to have hit a wall with arrays (either builtin or Javascript - not sure about either really) and keep getting an error on the following line:
modded = objectLoc + modLoc;
Unity says "Operator ‘+’ cannot be used with a left hand side of type ‘Object’ and a right hand side of type ‘UnityEngine.Vector3’. Both the objectLoc array and modLoc variable are declared as Vector3, so I’m lost as to why this error is cropping up?
I would appreciate any help whatsoever, especially any amendments that will turn this into a working script!
Thanks!

I’d recommend placing an empty gameobject at the player position, and parenting all your “distant” objects to that. Then you only need a script on the empty object that updates it’s position to match the player, and everything else will update automatically.

Have ran into this question and wrote an alternative solution.

/// <summary>
/// A class that makes object in 
/// the background appear very far 
/// away(sun, moon, planets, mountains) as they keep distance to 
/// the fromThis object that is holding the camera.
/// I guess it could be used for other purposes also.
/// </summary>
public class KeepDistance : MonoBehaviour
{
    [SerializeField]
    private GameObject fromThis;
    private Vector3 currentDistance;
    private Vector3 orginaDistance;
    private Vector3 diff;

    void Start()
    {
        orginaDistance = transform.position - fromThis.transform.position;
    }

    void Update()
    {
        currentDistance = transform.position - fromThis.transform.position;
        diff = orginaDistance - currentDistance;
        transform.position = transform.position + diff;
    }
}