Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Rotation optimization

Discussion in 'Scripting' started by MasmasDen, Nov 22, 2018.

  1. MasmasDen

    MasmasDen

    Joined:
    Nov 22, 2018
    Posts:
    2
    Hi everyone, I am only at the beginning with programming. I decided to make a Robot controller and wrote a code to rotate its parts, everything works greate, but I don’t know how to optimize this code or maybe there are some better solution to solve this issue?
    Sorry for my bad english.

    Code (CSharp):
    1.  
    2.  
    3. public PartInfo[] mechParts = new PartInfo[5];
    4.  
    5. void FixedUpdate () {
    6.             if (aiming == true)
    7.             {
    8.                 foreach (PartInfo part in mechParts)
    9.                 {
    10.                     if (part.canRotate == true && part.mechPart != null)
    11.                     {
    12.                         RotateParts(part.mechPart, part.rotSpeed, part.can_RotateX, part.limX, part.minX, part.maxX, part.angleX,
    13.                             part.can_RotateY, part.limY, part.minY, part.maxY, part.angleY);
    14.                     }
    15.                 }
    16.             }
    17.      }
    18.   private void RotateParts(Transform part, float rotSpeed, bool canX, bool limX, float minX, float maxX, float angleX, bool canY, bool limY, float minY, float maxY, float angleY)
    19.     {
    20.         Vector3 dir = aimPoint.position - part.transform.position;
    21.  
    22.         // Create new rotation towards the target
    23.         Quaternion TargetRotation = Quaternion.LookRotation(dir, Vector3.up);
    24.         Quaternion NewRotation = Quaternion.Slerp(part.rotation, TargetRotation, rotSpeed * Time.fixedDeltaTime);
    25.  
    26.         // Set the new rotation of the base.
    27.         part.rotation = NewRotation;
    28.  
    29.         if (canX == true)
    30.         {
    31.             //Clamp rotation
    32.             if (limX == true)
    33.             {
    34.                 angleX = Mathf.Clamp(ClampAngle(part.localEulerAngles.x),  maxX, minX);
    35.                 angleX = RoundFloatToDecimal(angleX, 2);
    36.             }
    37.             else
    38.             {
    39.                 angleX = RoundFloatToDecimal(part.localEulerAngles.x, 2);
    40.             }
    41.         }
    42.         else
    43.         {
    44.             angleX = 0;
    45.         }
    46.  
    47.         if(canY == true)
    48.         {
    49.             if (limY == true)
    50.             {
    51.                 angleY = Mathf.Clamp(ClampAngle(part.localEulerAngles.y),  maxY, minY);
    52.                 angleY = RoundFloatToDecimal(angleY, 2);
    53.             }
    54.             else
    55.             {
    56.                 angleY = RoundFloatToDecimal(part.localEulerAngles.y, 2);
    57.             }
    58.         }
    59.         else
    60.         {
    61.             angleY = 0;
    62.         }
    63.  
    64.         // Rotate
    65.         part.localEulerAngles = new Vector3(angleX, angleY, 0f);
    66.     }
    67.  
    68. [System.Serializable]
    69. public class PartInfo
    70. {
    71.     public string name;
    72.  
    73.     public Transform mechPart;
    74.  
    75.     [Header("Rotation Speed")]
    76.     public float rotSpeed = 5f;
    77.     public float returToIdleSpeedRot = 20f;
    78.  
    79.     [Header("Rotation Limits")]
    80.     public bool canRotate;
    81.     public bool can_RotateX; // if is true than enable limits X
    82.     public bool limX;
    83.     public bool can_RotateY; // if is true than enable limits Y
    84.     public bool limY;
    85.     //vertical roratition
    86.     [Range(10.0f, 90.0f)]
    87.     public float minX = 60f;
    88.     [Range(-10.0f, -90.0f)]
    89.     public float maxX = -60f;
    90.     public float angleX;
    91.     //horizontal rorattion
    92.     [Range(0.0f, 20.0f)]
    93.     public float minY = 4f;
    94.     [Range(0.0f, -20.0f)]
    95.     public float maxY = -4f;
    96.     public float angleY;
    97. }
    98.  
    99.  
     
    Last edited: Nov 22, 2018
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Welcome to Unity community.

    First optimization would be, add indentations to your code.
    However, if it works, what is your current concern?
     
  3. MasmasDen

    MasmasDen

    Joined:
    Nov 22, 2018
    Posts:
    2
    Thank you for your answer.
    I want to try to make the code cleaner.