Search Unity

Obvious Parenting localPosition issue

Discussion in 'Scripting' started by wxxhrt, Jun 23, 2014.

  1. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    Really sorry to post this- but can't figure it out myself!

    The code instantiates a circle of cubes around a centrepoint, rotates the cubes to look at that centre point then parents them to an empty at the centrepoint.

    I would then expect changing the cubes local position.z to move them toward and away from the centrepoint but thats not happening- can someone help- this seems like a really obvious problem I'm blind to. Thanks in advance :).

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var emptyPrefab : Transform;
    4. var cubePrefab    : Transform;
    5.  
    6. var noOfCubes     : int = 16;
    7. var radius        : float = 10.0;
    8. @HideInInspector
    9. var cubeArray     : Transform[];
    10.  
    11. var radiusBool  : boolean = false;
    12. function Start () {
    13.    
    14.     cubeArray = new Transform[noOfCubes];
    15.    
    16.     var xyzVec : Vector3 = Vector3(0, 0, 0);
    17.     var centrePoint : Transform = Instantiate(emptyPrefab, xyzVec, Quaternion.identity);
    18.                
    19.     var angle : float = (360.0 / noOfCubes) * Mathf.Deg2Rad;
    20.    
    21.     Debug.Log("angle " + angle);
    22.    
    23.    
    24.     for ( var m = 0; m < noOfCubes; m++ ) {
    25.        
    26.         var thisAngle : float = angle * m;
    27.        
    28.         Debug.Log(thisAngle);
    29.        
    30.         var x : float = xyzVec.x + radius * Mathf.Cos(thisAngle);
    31.         var y : float = xyzVec.y;
    32.         var z : float = xyzVec.z + radius * Mathf.Sin(thisAngle);
    33.        
    34.         var cube : Transform = Instantiate(cubePrefab, Vector3(x,y,z), Quaternion.identity);
    35.        
    36.         cube.LookAt(centrePoint);
    37.         cube.parent = centrePoint;
    38.         cubeArray[m] = cube;
    39.     }  
    40.    
    41.    
    42. }
    43.  
    44. function Update () {
    45.    
    46.     if (radiusBool){
    47.         for ( var m = 0; m < cubeArray.Length; m++ ) {
    48.             cubeArray[m].localPosition.z = radius;
    49.         }
    50.     }
    51. }
     
  2. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    Changing the localposition of z to radius on every cube will result in a line of cubes at that z coordinate. You still need to use angles like you did when you instantiated the cubes.
    Example:
    Code (csharp):
    1.   functionUpdate () {
    2.  
    3. if (radiusBool){
    4. for ( varm = 0; m < cubeArray.Length; m++ ) {
    5. thisAngle = angle * m;
    6. cubeArray[m].localPosition = Vector3(radius * Mathf.Cos(thisAngle), 0, radius * Mathf.Sin(thisAngle));
    7.  }
    8.  }
    9.  }
     
  3. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    Thanks, I had though about doing that but will be updating the radius' of around 600 objects eventually and was hoping for a speedier way of doing this.

    I had been under the impression that after rotating an object I could move it locally forwards or backwards by changing it's localPosition.z - am I wrong about this?:(

    Is there another way of achieving this without using what I imagine to be heavy cos and sin?

    Thanks
     
    Last edited: Jun 23, 2014
  4. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    In case anyone else finds this here is some code that works. It uses transform.Translate
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var emptyPrefab     : Transform;
    4. var cubePrefab        : Transform;
    5.  
    6. var noOfCubes         : int = 16;
    7. var radius            : float = 1.0;
    8. @HideInInspector
    9. var cubeArray         : Transform[];
    10. @HideInInspector
    11. var old_radius         : float;
    12. function Start () {
    13.    
    14.     cubeArray = new Transform[noOfCubes];
    15.    
    16.     var xyzVec : Vector3 = Vector3(0, 0, 0);
    17.     var centrePoint : Transform = Instantiate(emptyPrefab, xyzVec, Quaternion.identity);
    18.                
    19.     var angle : float = (360.0 / noOfCubes) * Mathf.Deg2Rad;
    20.    
    21.     for ( var m = 0; m < noOfCubes; m++ ) {
    22.        
    23.         var thisAngle : float = angle * m;
    24.            
    25.         var x : float = xyzVec.x + radius * Mathf.Cos(thisAngle);
    26.         var y : float = xyzVec.y;
    27.         var z : float = xyzVec.z + radius * Mathf.Sin(thisAngle);
    28.        
    29.         var cube : Transform = Instantiate(cubePrefab, Vector3(x,y,z), Quaternion.identity);
    30.        
    31.         cube.LookAt(centrePoint);
    32.  
    33.         cubeArray[m] = cube;
    34.         cube.parent  = centrePoint;
    35.     }
    36.  
    37.     old_radius = radius;      
    38. }
    39.  
    40. function Update () {
    41.    
    42.     if (radius != old_radius){
    43.        
    44.         var radiusDiff =  old_radius - radius;
    45.        
    46.         for ( var m = 0; m < cubeArray.Length; m++ ) {
    47.             cubeArray[m].Translate(0, 0, radiusDiff);
    48.         }
    49.        
    50.         old_radius = radius;
    51.     }
    52. }