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!