Search Unity

Question Сreation is not manual

Discussion in 'Scripting' started by Lux1606, Mar 28, 2022.

  1. Lux1606

    Lux1606

    Joined:
    Apr 18, 2020
    Posts:
    7
    Hello. Please teach me how to create child objects not by hand, but by a script. I understand the logic behind this action. (we make rays to the sides of the object and create the right amount, but I need to see and understand the code) thanks in advance. Attached an example of how I have now manually done.
    help.jpg
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Welcome to the wide world of procedural generation! It is incredibly rewarding and a VERY deep subject.

    I have been doing procedural generation since the very first game(s) I wrote back in the late 1970s and I am still learning new stuff even today.

    Unity makes procgen INCREDIBLY easy, easier than any other system I've used.

    Searching for "procedural generation unity" will turn you up TONS of tutorials.

    You're welcome to check out random examples in my MakeGeo project.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    Lux1606 likes this.
  4. Lux1606

    Lux1606

    Joined:
    Apr 18, 2020
    Posts:
    7
    Of course! I first studied the manual, and rather I incorrectly described the situation. I can create new objects in the directions I need. I need to make objects get created or removed when they hit a wall, eg: an object stands 5f away from the wall between it and the wall of 5 objects as soon as the object gets closer, eg 3f - objects will become 3
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Steps to success:

    - measure the distance to the wall (raycast? subtract positions? some other mechanism?)

    - compute how many items you want (distance divided by size of item)

    - adjust the given items within that gap (eg, create or destroy them to meet your count)
     
    Lux1606 likes this.
  6. Lux1606

    Lux1606

    Joined:
    Apr 18, 2020
    Posts:
    7
    Ok, I spawn from the parent cube object to 4 sides, then cast a ray from the child cube and so every frame => but the problem is that too many cubes are spawning. I'm sure the problem is in the condition
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    If you're sure, then fix it!

    If you're actually not sure, here's how to find out:

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    You must find a way to get the information you need in order to reason about what the problem is.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    Lux1606 likes this.
  8. Lux1606

    Lux1606

    Joined:
    Apr 18, 2020
    Posts:
    7
    Тo chance to understand now for me... This is parrent code
    Code (CSharp):
    1. public class testInst : MonoBehaviour
    2. {
    3.     public Transform prefab;
    4.     public float dist;
    5.     public LayerMask IgnoreMe;
    6.     void Start()
    7.     {
    8.  
    9.  
    10.         Left();
    11.         Right();
    12.         Forward();
    13.         Back();
    14.     }
    15.  
    16.     void Left()
    17.     {    
    18.         for (int i = 1; i < 2; i++)
    19.         {
    20.             Instantiate(prefab, -new Vector3(i * 1F, 0, 0), Quaternion.Euler(0,0,0), transform);
    21.  
    22.         }
    23.     }
    24.      void Right()
    25.     {
    26.      
    27.  
    28.         for (int i = 1; i < 2; i++)
    29.             {
    30.             Instantiate(prefab, new Vector3(i * 1.0F, 0, 0), Quaternion.Euler(0, 180, 0), transform);
    31.            
    32.         }
    33.     }
    34.     void Forward()
    35.     {
    36.      
    37.         for (int i = 1; i < 2; i++)
    38.         {
    39.            Instantiate(prefab, new Vector3(0, 0, i * 1.0F), Quaternion.Euler(0, 90, 0), transform);
    40.         }
    41.     }
    42.     void Back()
    43.     {
    44.      
    45.         for (int i = 1; i < 2; i++)
    46.         {
    47.             Instantiate(prefab, -new Vector3(0, 0, i * 1.0F), Quaternion.Euler(0, 270, 0), transform);
    48.         }
    49.     }
    50. }
    This is child.
    Code (CSharp):
    1. public class wallTrigger : MonoBehaviour
    2. {
    3.     public float distanceToWall;
    4.     public GameObject prefab;
    5.     public LayerMask IgnoreMe;
    6.  
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         RaycastHit hit;
    11.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, IgnoreMe))
    12.         {
    13.             distanceToWall = hit.distance;
    14.             Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left) * hit.distance, Color.yellow);
    15.  
    16.  
    17.             if (hit.transform != null)
    18.             {
    19.                 Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left) * hit.distance, Color.red);
    20.  
    21.                 if (transform.rotation.y == 0 && distanceToWall > 0.4)
    22.                 {
    23.                     for (int i = 1; i < distanceToWall; i++)
    24.                     {
    25.                         Instantiate(prefab, -new Vector3(i + 1.1f, 0, 0), Quaternion.identity, transform);
    26.  
    27.                     }
    28.  
    29.                 }
    30.  
    31.             }
    32.         }
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         RaycastHit hit;
    39.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, IgnoreMe))
    40.         {
    41.             distanceToWall = hit.distance;
    42.             Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left) * hit.distance, Color.yellow);
    43.  
    44.         }
    45.  
    46.         if(distanceToWall <= 0.49)
    47.         {
    48.             Destroy(gameObject);
    49.             Debug.Log("WALL");
    50.         }
    51.  
    52.  
    53.     }
    54. }
    55.