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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Resolved Help with Animating Rotation and Scaling

Discussion in 'Scripting' started by oshhhtv, Jun 27, 2021.

  1. oshhhtv

    oshhhtv

    Joined:
    Jun 20, 2021
    Posts:
    1
    Solved 5:40 pm EST
    Eventually found tutorials on how to edit colors of images and using timers to smoothly transform the scale and rotation. Updated source code below.

    I'm looking for a solution to have a hitmarker for my game rotate and scale down when the player hits an enemy. I have 4 different sprites for different kinds of damage inflicted and a script to assign the respective sprite to a UI image. The UI image is controlled by another object that I wish to apply the transformations to. Resizing and rotating the object instead of the UI image allows the sprite to retain its resolution and look clear even when scaled down or on a low resolution.

    The following script shows the step-by-step process of how I want the animation to play, with annotations describing what should happen in each step. I'm using an IEnumerator to use the WaitForSeconds method, so the hitmarker stays on the screen for a while. Essentially, I want to know if someone who's experienced with using rectTransform methods can tell me what statements I would need to write in order to get the expected result. I know what kinds of types to use, like Vector3 for scaling and rotation and Color32 for changing transparency, but I'm not sure how to apply them. Thanks in advance for the help!


    Code (CSharp):
    1.  
    2. private IEnumerator animateHit()
    3.     {
    4.         startTime = Time.time;
    5.  
    6.         //ready for animation
    7.         drawing = true;
    8.  
    9.         //increase scale to 0.1 XYZ
    10.         hitScale = 0.1f;
    11.         hitTransform.localScale = new Vector3(hitScale, hitScale, hitScale);
    12.  
    13.         //rotate sprite 12 degrees on Z axis
    14.         hitRotation = 12f;
    15.         hitTransform.eulerAngles = new Vector3(hitTransform.eulerAngles.x, hitTransform.eulerAngles.y, hitRotation);
    16.  
    17.         //make sprite opaque to show hitmarker
    18.         hitAlpha = 255f;
    19.         hitReference.color = new Color32(255, 255, 255, (byte)hitAlpha);
    20.  
    21.         //turn around z axis and scale down over 0.25 seconds
    22.         while (0.075f < hitTransform.localScale.x | 0f < hitTransform.eulerAngles.z)
    23.         {
    24.             elapsedTime = Time.time - startTime;
    25.  
    26.             if (0.075f < hitTransform.localScale.x) { hitTransform.localScale -= new Vector3(0.075f, 0.075f, 0.075f) * elapsedTime; }
    27.             if (0f < hitTransform.eulerAngles.z)
    28.             {
    29.                 hitRotation -= hitRotation * elapsedTime;
    30.                 hitTransform.eulerAngles = new Vector3(hitTransform.eulerAngles.x, hitTransform.eulerAngles.y, hitRotation);
    31.             }
    32.             yield return null;
    33.         }
    34.  
    35.         //wait for 0.25 seconds
    36.         //if it's a killing hit, wait for 1 second
    37.         if (hitReference.sprite == elim)
    38.         {
    39.             yield return new WaitForSeconds(0.25f);
    40.         }
    41.         else
    42.         {
    43.             yield return new WaitForSeconds(0.01f);
    44.         }
    45.  
    46.         startTime = Time.time;
    47.         //hide hitmarker by making sprite transparent, over 0.1 seconds
    48.         while (0f < hitAlpha)
    49.         {
    50.             elapsedTime = Time.time - startTime;
    51.  
    52.             Color temp = hitReference.color;
    53.             temp.a -= 1 * elapsedTime;
    54.             hitReference.color = temp;
    55.  
    56.             hitAlpha = hitReference.color.a;
    57.             yield return null;
    58.         }
    59.  
    60.         drawing = false;
    61.         yield break;
    62.     }
    63.  
     
    Last edited: Jun 27, 2021