Skip to main content
Added information that I should have included earlier.
Source Link

I am trying to make a script that lines up a list of GameObjects. Currently, I do this by generating a set of positions and then lerping the objects to them, the code works but does not account for the object's size so if it is large enough clipping will occur. I don't care if the solution is an improvement to the existing code or something entirely new. This is my current code:

private List GenLinePos(List objs) {

    float curHoldOffset = lineOffset;
    List<Vector3> positions = new List<Vector3>();
    positions.Add(lineAnchor.transform.position);
    int side = 1;
    for(int i = 1; i < objs.Count; i++)
    {
        //set initial position
        Vector3 pos = lineAnchor.transform.position;
        //change position
        pos += (lineAnchor.transform.forward * curHoldOffset) * side;
        side *= -1;
        if(side == 1)
        {
            curHoldOffset += lineOffset;
        }
        positions.Add(pos);
    }
    return positions;
}

Note: the line anchor is parented to the main camera which will be moving and rotating

I am trying to make a script that lines up a list of GameObjects. Currently, I do this by generating a set of positions and then lerping the objects to them, the code works but does not account for the object's size so if it is large enough clipping will occur. I don't care if the solution is an improvement to the existing code or something entirely new. This is my current code:

private List GenLinePos(List objs) {

    float curHoldOffset = lineOffset;
    List<Vector3> positions = new List<Vector3>();
    positions.Add(lineAnchor.transform.position);
    int side = 1;
    for(int i = 1; i < objs.Count; i++)
    {
        //set initial position
        Vector3 pos = lineAnchor.transform.position;
        //change position
        pos += (lineAnchor.transform.forward * curHoldOffset) * side;
        side *= -1;
        if(side == 1)
        {
            curHoldOffset += lineOffset;
        }
        positions.Add(pos);
    }
    return positions;
}

I am trying to make a script that lines up a list of GameObjects. Currently, I do this by generating a set of positions and then lerping the objects to them, the code works but does not account for the object's size so if it is large enough clipping will occur. I don't care if the solution is an improvement to the existing code or something entirely new. This is my current code:

private List GenLinePos(List objs) {

    float curHoldOffset = lineOffset;
    List<Vector3> positions = new List<Vector3>();
    positions.Add(lineAnchor.transform.position);
    int side = 1;
    for(int i = 1; i < objs.Count; i++)
    {
        //set initial position
        Vector3 pos = lineAnchor.transform.position;
        //change position
        pos += (lineAnchor.transform.forward * curHoldOffset) * side;
        side *= -1;
        if(side == 1)
        {
            curHoldOffset += lineOffset;
        }
        positions.Add(pos);
    }
    return positions;
}

Note: the line anchor is parented to the main camera which will be moving and rotating

Source Link

Lining up GameObjects in unity

I am trying to make a script that lines up a list of GameObjects. Currently, I do this by generating a set of positions and then lerping the objects to them, the code works but does not account for the object's size so if it is large enough clipping will occur. I don't care if the solution is an improvement to the existing code or something entirely new. This is my current code:

private List GenLinePos(List objs) {

    float curHoldOffset = lineOffset;
    List<Vector3> positions = new List<Vector3>();
    positions.Add(lineAnchor.transform.position);
    int side = 1;
    for(int i = 1; i < objs.Count; i++)
    {
        //set initial position
        Vector3 pos = lineAnchor.transform.position;
        //change position
        pos += (lineAnchor.transform.forward * curHoldOffset) * side;
        side *= -1;
        if(side == 1)
        {
            curHoldOffset += lineOffset;
        }
        positions.Add(pos);
    }
    return positions;
}