Search Unity

Problem with rotation on Instantiated objects

Discussion in 'Scripting' started by GlitchedEntity, Sep 11, 2019.

  1. GlitchedEntity

    GlitchedEntity

    Joined:
    Aug 5, 2019
    Posts:
    2
    Dear Forum Folks,
    I have a problem where I am continuously instaniating an object and I am setting the rotation equal to the rotation of the object that Instantiated it. Said Instantiator is moving forward and rotating, creating what I wanted to be a sort of tunnel of Instantiated objects (said objects are rings). However the tunnel is sort of flattenning out and not creating a tunnel like I wanted it to. Here is my code: (MakeRing is called from another script every so often)
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class EndlessGenerator : MonoBehaviour
    7. {
    8.     public float distance = 20;
    9.     private int rotX;
    10.     private int rotY;
    11.     private int rotZ;
    12.     private int rotAimX = 0;
    13.     private int rotAimY = 0;
    14.     private int rotAimZ = 0;
    15.     public int rotPower = 20;
    16.     public GameObject ring;
    17.     public EndlessObjective objective;
    18.     public GameObject MakeRing(bool rotate)
    19.     {
    20.         if (rotate)
    21.         {
    22.             for (int i = 0; i < rotPower; i++)
    23.             {
    24.                 if (!(rotX == rotAimX))
    25.                 {
    26.                     if (rotAimX > 0)
    27.                     {
    28.                         //transform.RotateAroundLocal(transform.up, 1);
    29.                         transform.Rotate(transform.up, 1);
    30.                         rotX += rotPower;
    31.                     }
    32.                     else
    33.                     {
    34.                         transform.Rotate(transform.up, -1);
    35.                         rotX -= rotPower;
    36.                     }
    37.                 }
    38.                 else
    39.                 {
    40.                     rotAimX = Random.Range(-180, 181);
    41.                 }
    42.                 if (!(rotY == rotAimY))
    43.                 {
    44.                     if (rotAimY > 0)
    45.                     {
    46.                         //transform.RotateAroundLocal(transform.up, 1);
    47.                         transform.Rotate(transform.right, 1);
    48.                         rotY += rotPower;
    49.                     }
    50.                     else
    51.                     {
    52.                         transform.Rotate(transform.right, -1);
    53.                         rotY -= rotPower;
    54.                     }
    55.                 }
    56.                 else
    57.                 {
    58.                     rotAimY = Random.Range(-180, 181);
    59.                 }
    60.                 if (!(rotZ == rotAimZ))
    61.                 {
    62.                     if (rotAimZ > 0)
    63.                     {
    64.                         //transform.RotateAroundLocal(transform.up, 1);
    65.                         transform.Rotate(transform.forward, 1);
    66.                         rotZ += rotPower;
    67.                     }
    68.                     else
    69.                     {
    70.                         transform.Rotate(transform.forward, -1);
    71.                         rotZ -= rotPower;
    72.                     }
    73.                 }
    74.                 else
    75.                 {
    76.                     rotAimZ = Random.Range(-180, 181);
    77.                 }
    78.             }
    79.         }
    80.         transform.Translate(transform.forward * distance);
    81.  
    82.         GameObject ringInstance = Instantiate(ring);
    83.         ringInstance.transform.position = transform.position;
    84.         ringInstance.transform.rotation = transform.rotation;
    85.         ringInstance.GetComponent<MeshRenderer>().material = objective.inactiveRing;
    86.         ringInstance.transform.parent = objective.gameObject.transform;
    87.        
    88.        
    89.         return ringInstance;
    90.     }
    91.    
    92. }
    93.  
    94.  
    I think it's a problem with the rotating part, but I am utterly lost and I'm way out of my depth here. Thanks in advance!
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    633
    What is the effect you are trying to achieve? It seems to me you are taking unnecessary steps by moving/rotating an object and spawning at its new position/rotation.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,511
    It's hard to get a handle on the issue without seeing a screenshot of the problem. I can say 2 things for sure though:
    1) the tunnel "flattening out" almost certainly means it's rotating on the wrong axis somehow.
    2) Your rotation code could be a LOT simpler if you used Mathf.MoveTowards or Quaternion.RotateTowards. (and simpler code is easier to debug) It certainly doesn't need to be a loop. Lines 23-79 can be replaced by like 3 lines of code. Something like this (untested):
    Code (csharp):
    1.  Quaternion rotAim = Quaternion.identity;
    2. public float rotPower = 20f;
    3.  
    4. public GameObject MakeRing(bool rotate) {
    5. if (rotate)
    6. {
    7. transform.rotation = Quaternion.RotateTowards(transform.rotation, rotAim, rotPower);
    8. if (transform.rotation == rotAim)
    9. {
    10. rotAim = rotAim * Quaternion.Euler(Random.Range(-180f, 180f),Random.Range(-180f, 180f),Random.Range(-180f, 180f) );
    11. }
    12. transform.Translate(Vector3.forward * distance);
    13. //...and continue
    3) Just kidding there's a third thing! This is probably your actual problem, I noticed it (and fixed it) while typing the above. transform.Translate uses the transform's rotation, so when you fed into it transform.forward, then on the instantiator's movement, it was effectively doubly applying the rotation. So the rings were fine, but the instantiator was "sliding" sideways.
    Use transform.position += transform.forward *distance; or transform.Translate(Vector3.forward * distance);
     
    GlitchedEntity likes this.
  4. GlitchedEntity

    GlitchedEntity

    Joined:
    Aug 5, 2019
    Posts:
    2
    This really helped & I'm now using it (and the Quaternion.RotateTowards thing), and it really optimized the code and made it a lot less bulky. Thanks a lot!