Search Unity

Request for script ... start random movement

Discussion in 'Scripting' started by DaveyJJ, May 16, 2005.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    When a scene opens, I need an object to begin moving randomly in both the X and Z axis with a random speed/force. That might also be one of the included standard scripts. I may try writing this one myself.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You want to implement this in the Awake function.
    eg.

    void Awake ()
    {
    float x = Random.RandomRange (-1, 1);
    float z = Random.RandomRange (-1, 1);
    rigidbody.velocity = new Vector3 (x, 0, z);
    }
     
  3. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    Regarding the above script: Is there an easy way to constrain a randomly moving object so it doesn't move off the map? In other words, is there a way to make virtual walls such that it won't simply vanish into the distance over time?

    I'd like to have NPCs move around the terrain randomly, but stay within a predefined area.

    Thanks for the help.
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    An easy way is to go about the task slightly differently. Instead of moving the object in a random direction, choose a random point and move toward it (then, when you get there, choose another point and go toward that, etc). This doesn't work if the area has internal walls, though. You could also try adding a collider around the border of the area. When an object hits it, you can detect the collision by adding an OnCollisionEnter function to your script. The function would contain code to set the object off in another direction.
     
  5. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    I'm not sure how to make an object move to a random point, but I'll look at the scripting reference and see if I can figure it out.

    Regarding the idea of putting a collider around an area, do you mean a mesh collider? Guess I'm not clear on how this would be done. I'm fairly new to all of this. It would be interesting to have objects hit a collider, then rotate a random amount and continue on their way until they hit another collider, then turn again etc.

    Many thanks for your help! :roll:
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If the area is circular, you can use Random.insideUnitCircle to get a random point within a radius of one unit (you multiply this by the desired radius to get your result). If it's a rectangular area, you can generate a random X and Y coordinate within the right range.

    If your area is an irregular shape then you could have some kind of "wall" object around it (possibly invisible) and add a MeshCollider to that. If you add the OnCollisionEnter function to your script, this method will be called whevever the object touches another collider. You could set your objects to move in a random direction and then detect when they hit the wall using OnCollisionEnter. You would put some code in this function to turn the colliding object around and set it off in its new direction.
     
  7. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    Thanks so much. I'll give both methods a try and see which works best.
     
  8. aemason

    aemason

    Joined:
    Dec 16, 2011
    Posts:
    4
    "Regarding the above script: Is there an easy way to constrain a randomly moving object so it doesn't move off the map?" - Tool55

    @tool55 I use transparent cubes that stretch the map length, set them as triggers. for my asteroids game my trigger makes the asteroids bounce off with a -velocity.

    void OnTriggerEnter(Collider other)
    {
    other.rigidbody.velocity = -other.rigidbody.velocity;
    }


    "If it's a rectangular area, you can generate a random X and Y coordinate within the right range." -andeeee

    @ andeeee This worked GREAT!


    oh this is an old post. lol.
     
    Last edited: Dec 27, 2011