Search Unity

Spreading objects randomly..

Discussion in 'Scripting' started by robertseadog, Sep 14, 2005.

  1. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Is there a way to initialize a object randomly across a surface with Javascript? If there is, could you show it to me or make suggestions what I have to look up in order to script something like that?

    :roll:

    Thanks!
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Surface as in, a mesh?
     
  3. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Thats the idea!
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  5. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Ah, I'll try to cook up something and get back with some real questions :wink:
     
  6. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Could something like this work?
    Code (csharp):
    1.  
    2. //-- Assign to Emty Obj to make it initiate medkits around the scene:
    3. //-- Var's:
    4. var initMedKit : GameObject;
    5.  
    6. private var medkitTransform = 0;
    7. private var medkitCount = 0;
    8. private var totalmedkitCount = 10;
    9.  
    10. //-- Func's:
    11. function Start () {
    12. endlessLoop ();
    13. }
    14. function objMadman () {
    15.     if (medkitCount < totalmedkitCount) {
    16.    
    17.     //-- assigns a var to the initiated medkit's transforms:
    18.     medkitTransform = hit.point;
    19.  
    20. //-- initiate a medkit at the position it should be:
    21.     instantiatedExplosion = Instantiate (initMedKit, medkitTransform, transform.rotation);
    22.     medkitCount++;
    23. //-- Figure out the forward look of the medkit:
    24.     var forwardLook = Vector3.Cross (hit.normal, instantiatedExplosion.TransformDirection (Vector3.left));
    25.  
    26. //-- Build a rotation and assign to the medkit:
    27.     instantiatedExplosion.rotation = Quaternion.LookRotation (forwardLook, hit.normal);
    28.     }
    29. }
    30. function endlessLoop () {
    31. //-- By the way; will this sort of looping cause strain to the computer? Is there a better way?
    32. moveObj ();
    33. }
    34. function moveObj () {
    35. yield WaitForSeconds (5.0);
    36. transform.position = Vector3 ( Random.Range (5, 40), 0, Random.Range (5, 40));
    37. endlessLoop();
    38. }
    39. function Update () {
    40. //-- cast a ray down from the emty-object
    41. var hit : RaycastHit;
    42.  
    43.     if (Physics.Raycast (transform.localPosition, transform.TransformDirection (Vector3.down), hit)) {
    44.     objMadman ();
    45.     }
    46. }
    47.  
    But the problem with that is that I've got no way to hit only the desired surfacemesh, the idea here is that the emty object just trasfer itself around the scene like a madman, initiating obj all over the place! But I really need to only initiate obj on 1 surface; the floor!

    :D
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Use layer masks to cast rays against only one object or group of objects.
    http://www.otee.dk/Documentation/ScriptingConcepts/Layers.html

    You can also use the Renderer.bounds of the object to constrain where you want to cast rays downwards.

    Also you don´t need to change the transform, you could simply generate a Vector3 and use that for casting the ray directly.

    eg.

    castPosition = Vector3(Random.Range(0, 10 ), 0, Random.Range(0, 10 ));

    Then you use castPosition as the origin in the raycast instead of transform.position.
     
  8. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    And regarding endless loops, it's much more straightforward to use a while loop than infinite recursion:
    Code (csharp):
    1.  
    2. function LoopDeLoop () {
    3.     while(true){
    4.        //do something
    5.        yield ....;
    6.    }
    7. }
    8.  
    ... the recursion trick might also overrun your stack space, as mono allocates a stack frame for every function call, but since you never return, the stack never shrinks back. (We might optimise for tail-recursion, but I'm not sure if we do or whether it would kick in in your case.)
     
  9. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Ah, thank you for the great input! :wink:

    Joachim -> I don't really understand the Renderer.bounds method? Do you have any examples of that( do you use obj.Renderer.bounds or how do I access it? and what does it return?)
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    From the script reference documentation of 1.1.1:

    The bounding volume of the renderer. This is the axis-aligned bounding box containing the mesh in world space.

    // Prints the left extreme point of the bounding volume on the x-axis
    print(renderer.bounds.min.x);
    // Prints the right extreme point of the bounding volume on the x-axis
    print(renderer.bounds.max.x);
     
  11. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Thanks! I'll try it..
     
  12. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    This will be soooo much easer once we have access to the mesh atthe face level! ;-)
     
  13. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    Then I'll hold my breath and wait for Scheme! :D