Search Unity

How could I combine both of these scripts into one?

Discussion in 'Scripting' started by Treasureman, Feb 10, 2016.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have 2 scripts that make a UI Image rotate. I want to make these 2 scripts into one because they both work individually, but together, it really make the game glitch out. Here are the 2 scripts...
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class MenuArrowY : MonoBehaviour
    6. {
    7.     public float normalAngle = 180;
    8.     public float smooth = 100;
    9.     public Image imageToLerp;
    10.  
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         Vector3 euler = imageToLerp.transform.eulerAngles;
    21.         float hInput = Input.GetAxis("Vertical");
    22.  
    23.         if (hInput < 0)
    24.         {
    25.             euler.z = Mathf.LerpAngle(euler.z, 180, Time.deltaTime * smooth);
    26.         }
    27.         else if (hInput > 0)
    28.         {
    29.             euler.z = Mathf.LerpAngle(euler.z, 0, Time.deltaTime * smooth);
    30.         }
    31.         else
    32.         {
    33.             euler.z = Mathf.LerpAngle(euler.z, normalAngle, Time.deltaTime * smooth);
    34.         }
    35.  
    36.         imageToLerp.transform.eulerAngles = euler;
    37.     }
    38. }
    Code (CSharp):
    1. using UnityEngine;
    2.     using UnityEngine.UI;
    3.     using System.Collections;
    4.  
    5.     public class MenuArrowH : MonoBehaviour
    6.     {
    7.         public float normalAngle = 180;
    8.         public float leanAngle = 90;
    9.         public float smooth = 100;
    10.         public Image imageToLerp;
    11.  
    12.         // Use this for initialization
    13.         void Start()
    14.         {
    15.  
    16.         }
    17.  
    18.         // Update is called once per frame
    19.         void Update()
    20.         {
    21.             Vector3 euler = imageToLerp.transform.eulerAngles;
    22.             float hInput = Input.GetAxis("Horizontal");
    23.  
    24.             if (hInput < 0)
    25.             {
    26.                 euler.z = Mathf.LerpAngle(euler.z, leanAngle, Time.deltaTime * smooth);
    27.             }
    28.             else if (hInput > 0)
    29.             {
    30.                 euler.z = Mathf.LerpAngle(euler.z, -leanAngle, Time.deltaTime * smooth);
    31.             }
    32.             else
    33.             {
    34.                 euler.z = Mathf.LerpAngle(euler.z, normalAngle, Time.deltaTime * smooth);
    35.             }
    36.  
    37.             imageToLerp.transform.eulerAngles = euler;
    38.         }
    39.     }
    I really just want the UI Image to rotate to all 8 angles. If you don't know what I mean, just try out the scripts, you'll understand what I mean.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    so if they use horizontal axis you spin 180, if they use vertical axis you spin 90? is that essentially the only difference between the two scripts?
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Okay, so, what I really just want to do is to have a UI Image lerp in the direction you hold the Horizontal and Vertical Axies (axis?, axises?, whats the plural for axis?).
     
  4. KingArri

    KingArri

    Joined:
    Jul 2, 2015
    Posts:
    3
    just use two different names for the vertical and horizontal axis and keep it all in one file
    and change the if statements:
    Code (csharp):
    1. if(hInput < 0 && vInput < 0)
     
    Last edited: Feb 10, 2016