Search Unity

Question loading "animation" after stepping on the pressure plate

Discussion in 'Scripting' started by foxyyhappyw, Mar 15, 2023.

  1. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    61
    what's the best way to do a loading "animation" (e.g. color change) on a gameobject?

    I mean something like the attached picture.
    I wish this yellow "cable" had its loading animation. To change its color from gray to yellow.

    I wouldn't want to do it with an animator tool, I'd prefer a script.
     

    Attached Files:

    • xd.png
      xd.png
      File size:
      38.1 KB
      Views:
      81
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    Color.lerp ?
     
    Kurt-Dekker likes this.
  3. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    61
    as far as i know, color.lerp will only change the color smoothly, but it won't provide animation like in the picture.
     
  4. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    Hmm , i guess i missed something, but i dont see any animation, just a static image.
     
  5. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    61
    yeah, it is a static image. but these red arrows are supposed to mean how this "animation" is supposed to work, I don't know how to describe it better xd
     
  6. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    ohhhh, you want the color to smoothly go around the outside edge (faces, whatever you prefer) as its loading? First thing that comes to mind, is using a gradient and evaluating it over time. But i havent really attempted something like that, i just think i could make it hapen with a bit of poking.
     
    foxyyhappyw likes this.
  7. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    61
    ooo yes, exactly
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I've been meaning to make a little circular / square loading demo like this for a while now so I did. See attached. Geometry has been hand-UV-unwrapped in Blender, exported also as an FBX for use in the scene.

    Screen Shot 2023-03-15 at 7.53.41 AM.png

    Here's the salient script:

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. // @kurtdekker - animates the fill level by changing
    6. // the material main offset.
    7. // Requires that you have UV-mapped the shape correctly!
    8.  
    9. public class SetFillLevel : MonoBehaviour
    10. {
    11.     [Header("Provide or else we GetComponent()")]
    12.     public Renderer TargetRenderer;
    13.  
    14.     [Header( "Set at start: 0 to 1")]
    15.     public float currentFill;
    16.     [Header( "Set at runtime to change: 0 to 1")]
    17.     public float desiredFill;
    18.  
    19.     Material mtl;
    20.  
    21.     const float LoadSpeed = 2.0f;
    22.  
    23.     void Start ()
    24.     {
    25.         Debug.Log( "Press 1 or 2 for 0 or 100% loaded.");
    26.  
    27.         // if you forget, we'll look here where we are
    28.         if (!TargetRenderer)
    29.         {
    30.             TargetRenderer = GetComponent<Renderer>();
    31.         }
    32.  
    33.         mtl = TargetRenderer.material;
    34.  
    35.         desiredFill = currentFill;
    36.  
    37.         DriveToMaterial();
    38.     }
    39.  
    40.     void DriveToMaterial()
    41.     {
    42.         // zero is half-loaded the way I UV-mapped it
    43.         float y = currentFill - 0.5f;
    44.  
    45.         Vector2 offset = new Vector2( 0, y);
    46.  
    47.         mtl.mainTextureOffset = offset;
    48.  
    49.         TargetRenderer.material = mtl;
    50.     }
    51.  
    52.     void Update ()
    53.     {
    54.         if (Input.GetKeyDown( KeyCode.Alpha1))
    55.         {
    56.             desiredFill = 0;
    57.         }
    58.         if (Input.GetKeyDown( KeyCode.Alpha2))
    59.         {
    60.             desiredFill = 1;
    61.         }
    62.  
    63.         currentFill = Mathf.MoveTowards( currentFill, desiredFill, LoadSpeed * Time.deltaTime);
    64.  
    65.         DriveToMaterial();
    66.     }
    67. }
     

    Attached Files:

    foxyyhappyw and Homicide like this.
  9. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    Nice one, Kurt. That about sums it up, and saves me from poking around at all. :D
     
    Kurt-Dekker likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Thanks! I find that by doing even simple little full-stack Unity things like this (things that exercise scene, canvas, material, renderer, importer, Blender3D, export, and a sprinkling of code) it helps me do my day job a lot better, where I might not need to touch vast portions of that stack for weeks on end. :)
     
  11. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    61
    oh nice, that's exactly what I meant, thanks a lot!
     
    Kurt-Dekker likes this.