Random transform question- probably an easy fix ;-)

Hi, all,

I’m trying to randomly transform the position of a 2D object, constrained to a 10 x 10 area. The following script almost works, but the object relocates at runtime then randomly moves for a time then drifts off (slowly) into infinity. Any suggestions how I can get this working?

Thanks!

var time = 1;
function FixedUpdate () {
time ++;
if (time == 10) {
var xRandMove : float = (Random.Range(-.5, .5));
var yRandMove : float = (Random.Range(-.5, .5));
var zRandMove : float = (Random.Range(-.0, 0));
rigidbody.AddForce (xRandMove, yRandMove, zRandMove);
time = 1;
}
}

No errors in the console?

What’s with the random -0 ↔ 0?

var time = 1;
function FixedUpdate () {
time ++;
if (time == 10) {
  var xRandMove : float = (Random.Range(-.5, .5));
  var yRandMove : float = (Random.Range(-.5, .5));
  var zRandMove : float = (0.0);
  rigidbody.AddForce (xRandMove, yRandMove, zRandMove);
  time = 1;
}

you had it randomly setting a number between -.0 and 0 which might give some strange results try it now

Thanks, guys. I’m using your corrections, but am still experiencing some odd behavior, most notably, a drifting, as if the script ceased to function. The object also reorients itself slightly in the z axis. Any ideas?

var time = 1;
function FixedUpdate () {
time ++;
if (time == 10) {
  var xRandMove : float = (Random.Range(-.5, .5));
  var yRandMove : float = (Random.Range(-.5, .5));
  var zRandMove : float = (0.0);
  rigidbody.AddForce (xRandMove, yRandMove, zRandMove);
  time = 1;
} 
}

So - [quote=“”]
No errors in the console?
[/quote]
?

here I am going to try that code and see what I can do.

I just put that code into unity and it goes off screen up and down and left and right.

it should eventually if you keep doing that because every time time == 10 it applies force to it. In a random way but it still would. I also don’t have the smooth drifting thing have you looked at the rigidbody? it could be gravity or some other setting. like drag or something like that.

[Edit]

now I do, but only because I had set mass to 0, try setting it to say 100 and it shouldn’t happen

here is what is happening:

  1. you push the cude in a random way.

  2. the physics engine interprets this and moves the cude that way for the amount of force and masss and all those other things. I have a feeling it is mass.

unless you mean the part where it moved off screen like it was moving in 3d.

That I have no idea why.