Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mirror Objects At Runtime (Symmetry over X and Z Axis)

Discussion in 'Scripting' started by SiMULOiD, May 3, 2020.

  1. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Hi,
    The objects in this script are mirrored over 1 axis only, but I need them to mirror over both X and Z axis. I'd also like to to assign each object a starting point around a circle.
    Suggestions?



    Code (CSharp):
    1. using UnityEngine;
    2. public class MirrorObject : MonoBehaviour
    3. {
    4.     public Transform ObjA;
    5.     public Transform ObjB;
    6.     public Transform MirrorPoint;
    7.     private void Update()
    8.     {
    9.    
    10.          ObjB.position = Vector3.LerpUnclamped(ObjA.position,  MirrorPoint.position, 2);
    11.  
    12.     }
    13. }
     
    Last edited: May 3, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Technically in the code above, you're not mirroring that over one axis, you're mirroring it around a point. :)

    In any case, to distribute things around a circle, it's pretty simple math. You iterate the number of items you want, and rotate an offset distance around a circle, something like so:

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DistributeAroundCircle : MonoBehaviour
    6. {
    7.     void Start ()
    8.     {
    9.         int numItems = 7;
    10.  
    11.         Vector3 center = Vector3.zero;
    12.  
    13.         float radius = 3.0f;
    14.  
    15.         // if you don't want the first object at angle = 0,
    16.         // set this to some offset, and all objects will be
    17.         // "slewed" around the circumference.
    18.         float angularOffset = 0.0f;
    19.  
    20.         for (int i = 0; i < numItems; i++)
    21.         {
    22.             // calculate angle in degrees, based on numItems:
    23.             float angle = (i * 360.0f) / numItems;
    24.  
    25.             // offset it?
    26.             angle += angularOffset;
    27.  
    28.             // choose the rotation axis (I chose z here)
    29.             Quaternion rotation = Quaternion.Euler( 0, 0, angle);
    30.  
    31.             // get the position away from zero. Vector3.up is chosen
    32.             // because it is orthogonal to the rotation axis I chose above.
    33.             Vector3 pos = rotation * Vector3.up * radius;
    34.  
    35.             // add in the center
    36.             pos += center;
    37.  
    38.             var go = GameObject.CreatePrimitive( PrimitiveType.Sphere);
    39.             go.transform.position = pos;
    40.         }
    41.     }
    42. }
     
  3. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Thanks, Kurt, I appreciate the solution- that’s basically what I was searching for.
     
    Kurt-Dekker likes this.