Search Unity

Programmatically rendering prefabs and connecting them together

Discussion in 'General Graphics' started by unity_05B59064C1E7A73BFCFC, Sep 6, 2021.

  1. unity_05B59064C1E7A73BFCFC

    unity_05B59064C1E7A73BFCFC

    Joined:
    Sep 1, 2021
    Posts:
    5
    New to the wonderful Unity 3D engine and it didn't take me long before I had to come ask the community for some help!

    I'm trying to render prefabs at runtime and link them together to form a sort of never-ending tunnel. I'm keeping it simple for now and have made it so each prefab is a tunnel segment that consists of a hollow rectangular cuboid that contains exactly two quads at each end with static names. The quads are meant to act as connectors for each segment.

    Using the diagram I attached as a visual guide, how do I link two objects together such that their connecting quads match each other perfectly? Please disregard the fact that the object on the left-hand side is slightly curved.

    More specifically, I do not know how to rotate and translate an object (object II in the diagram) in such a way that its connecting quad matches the position of another object's quad (object I in the diagram).

    Please feel free to suggest better ways of accomplish this if you think my strategy is wrong or not performant/elegant enough.
     

    Attached Files:

  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    so do you want to merge or bridge/cap actual vertices together,
    or just to spawn and move/rotate the objects so that the edges are touching?
     
  3. miguel_unity866

    miguel_unity866

    Joined:
    Sep 1, 2021
    Posts:
    1
    The idea is to spawn an object and move/rotate it so that its connector quad perfectly touches the connector quad of an existing object in the scenegraph.

    The problem I'm having is my knowledge of math is super limited. I just don't know what sort of operations I need to do to calculate the rotation and translation of the spawned object.
     
  4. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    If the source quads are aligned, then copying the transform.rotation of the new object to the old object will align it perfectly. You can also use a transform's different facing vectors to get where it's pointing. So let's say your objects are 6 units wide and their handle is their centre. You have your first object at (0,0,0) and it's pointing in a random direction. You can create the new object 6 Unity units away along the first object's .forward axis. Something like (off the top of my head, not genuine code):
    Code (CSharp):
    1.  
    2. GameObject newPiece = Instantiate(TunnelPiece);
    3.  
    4. newPiece.transform.position = oldPiece.transform.position + oldPiece.transform.forward*6;
    5. newPiece.transform.rotation = oldPiece.transform.rotation;
    Then the next piece would be placed at the last piece's position plus its facing vector times the length you need to move, and match the rotations.

    Note the facing vectors, .up, .right, .forward, etc., are unit vectors so always length one. Simply times them by the distance you want to move along those directions for positions relative to a transform.