Moving an object randomly in a defined space, without collision

Hi,

I’ve created a simple object and i want it to move freely in an invisible close space (so that a camera can always see it).

This movement should be smooth, of a changing speed (see the “vitRdm” below) and whenever the object reaches a “wall” from this space, it would just change direction a little bit before but no rebound on it (no collision).

I’m quite new at Unity and so far i’ve only be able to create this object (a simple sphere), apply it a movement in a close space :

var vitRdm = 10;

function Update() {
        vitRdm = Random.Range(8.0,9.0);
        transform.position = Vector3(Mathf.PingPong(Time.time*vitRdm,9.9),Mathf.PingPong(Time.time*vitRdm,9),Mathf.PingPong(Time.time*vitRdm,8));
    }

BTW, if I set RandomRange to (8.0, 10.0), my sphere starts having an odd behaviour: it starts shaking instead of moving at a changing speed. Why ?

Thank you very much for your help.

Cheers

Just check this below UnityScript. I created it before so long back. Also check your profiler, its CPU eater or not…:

var vx;
var vy;
var vz;

function Awake(){
	vx=Random.Range(-0.05,0.05);
	vy=Random.Range(-0.03,0.03);
	vz=Random.Range(-0.01,0.01);
}
function Update(){
	transform.position.x +=vx;
	transform.position.y +=vy;
	transform.position.z +=vz;
	
	if (transform.position.x<-7.0){
		vx=vx*-1;
	}else if (transform.position.x>7.0){
		vx=vx*-1;
	}
	
	if (transform.position.y<-4.0){
		vy=vy*-1;
	}else if (transform.position.y>6.0){
		vy=vy*-1;
	}
	if (transform.position.z<-3.0){
		vz=vz*-1;
	}else if (transform.position.z>2.0){
		vz=vz*-1;
	}	
	var fwd = transform.TransformDirection (Vector3.forward);
	if (Physics.Raycast (transform.position, fwd, 10)) {
		print ("There is something in front of the object!");
	}
}

Your code does a pretty good job, but the ball always goes straight forward then bounce in another direction while i’d love it to go on a curved path.