Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

random motion

Discussion in 'Scripting' started by BlitheD, Oct 14, 2006.

  1. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    I have the below script that I was hoping would give me random motion. It doesn't. I thought the "RandMove" variables would be changed each time "time" equals ten but every time I test it, the object just gets set going the same way (it actually seems to be orbiting around a central point). Could someone tell me where I am messing this up?
    Thanks


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

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Works fine here, I think, although I don't know exactly what sort of behavior you're after. Since you're using AddForce, the random forces are applied to the force it already had before. In other words, if it has a movement of, say, (5, -3, 4), and you get a random number of -.5, 0, .25, you'd end up with (4.5, -3, 4.25). So it would look like it was mostly going in pretty much the same direction as before. Over time, of course, it will change, but you won't get any drastic changes in direction. Like I said, not sure if that's what you want or not.

    BTW, it's probably a good idea to get into the habit of defining types. Not only is it faster, since they're figured at compile time instead of run time, but you can avoid some potentially confusing situations. For example, if you decided to change "var xRandMove = (Random.Range(-.5, .5))" to "var xRandMove = (Random.Range(-5, 5))", you'd get integers back instead of floats. And if you were expecting to still get floats, you might wonder what happened. It's better to use "var xRandMove : float = (Random.Range(-.5, .5))" just to be sure (and add a bit of speed). No, I didn't have to learn that the hard way...cough cough.... ;)

    --Eric
     
  3. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    >Since you're using AddForce, the random forces are applied to the force it already had before.

    I see. I wasn't considering that... That helps me understand things a lot better.

    >BTW, it's probably a good idea to get into the habit of defining types...

    Hmm. I don't understand how that makes it faster but I will take your word for it and try to implement that kind of scripting.
    Thanks for all of your advice. (I guess getting problems solved on this forum would qualify as one of "the easy ways"? It's certainly saved me a lot of work...)
     
  4. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Incidentally, Random.insideUnitSphere() is a nice shorthand way of getting a randomly oriented, random length (up to one unit long) vector. It's probably preferable to getting random numbers for each axis, as that would allow a greater maximum speed along the diagonals.
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Actually it is not added to the force, it is added to the velocity.

    AddForce adds forces during one frame. So if you have 2 explosions and call addforce on an object twice, then both forces will be applied.
    During the physics simulation step, this force is applied to the velocity and the accumulated force is set to zero. Thus if you plan to apply a force to an object constantly you have to call AddForce every frame.

    Now regarding the script. I don't know what effect you are exactly after but if i disable the gravity on the rigidbody the object will move around pretty randomly.

    Increasing the random force to be applied to something more like 10 helps a lot. Of course since there is no drag on the object the velocity will not modify the velocity that much. Maybe just increasing the drag gives the effect you are after. Or just using rigidbody.velocity = Vector3.zero; when applying the force.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's because if you don't define the type, when the game is run, it comes across a variable and says, "OK, here's a variable...now, I have to stop for a bit and figure out if it's an integer, or a vector3, or whatever." But if you define the type, the compiler does the "stop for a bit and figure out" thing when the code is compiled, which means that the game already knows the type when running, so it skips over that step. Granted you probably wouldn't be able to tell the difference in most situations, but it all adds up in the end....

    --Eric
     
  7. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    But you forgot that Unity's javascript does type inference.
    When you assign something with a known type to something with a previously unknown type, the compiler will automatically infer the type.

    In fact, the code snippet posted above is fully statically typed.
    time is an integer because of the 'var time = 1;' statement and xRandMove, yRandMove and zRandMove get their types from Random.Range.

    I would not recommend adding explicit type definitions blindly, as leaving them out often produces more readable code. (Not to say less typing.)
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I didn't forget...because I didn't know. :) That's cool that it does that.

    --Eric
     
  9. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Ah I just assumed everybody knew that :p ... it is one of the most useful features of Unity's Javascript (and Boo) in my opinion, alas not mentioned very much on the Unity pages.