I have a object that moves with a script.
...
transform.Translate (0.01* XVel * Time.deltaTime;, 0.01*YVel * Time.deltaTime, 0.01*ZVel * Time.deltaTime, Space.World);
...
To this mesh is attached a child object that moves with another script.
var OBJ : GameObject;
var OBJPos : Vector3;
var OBJPosIN : Vector3;
var maxX : float;
var minX : float;
var maxY : float;
var minY : float;
var moveSpeed : float = 1.0;
var tChange: float = 0; // force new direction in the first Update
var randomX: float;
var randomY: float;
function Start () {
OBJPosIN = OBJ.transform.position;
}
function Update () {
OBJPos = OBJ.transform.position - OBJPosIN ;
// change to random direction at random intervals
if (Time.time >= tChange){
randomX = Random.Range(-2.0,2.0); // with float parameters, a random float
randomY = Random.Range(-2.0,2.0); // between -2.0 and 2.0 is returned
// set a random interval between 0.5 and 1.5
tChange = Time.time + Random.Range(0.5,1.5);
}
transform.Translate(Vector3(randomX,randomY,0) * moveSpeed * Time.deltaTime, Space.Self);
// if object reached any border, revert the appropriate direction
if (transform.position.x >= maxX || transform.position.x <= minX) {
randomX = -randomX;
}
if (transform.position.y >= maxY || transform.position.y <= minY) {
randomY = -randomY;
}
// make sure the position is inside the borders
transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX) ;
transform.position.y = Mathf.Clamp(transform.position.y, minY, maxY) ;
transform.position += OBJPos;
}
I want the son to follow the parent object while still moving with his script.
I try to insert position of parent object but the children object only move correctly in Y axis.
Where I’m wrong?