Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

move truck and change coloraturas over time

Discussion in 'Scripting' started by Kiesco91, Aug 24, 2020.

  1. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Hi All,

    so ive created a project for work to improve our processes but im struggling on some asthetics.

    basically, I want to type in the number of the truck that arrives in work (I work in distribution) then have a screen to show how long they have been waiting to be loaded that I can reset once they have been loaded.

    I have the basics working (input field, truck number, timer) but I would like the truck to slowly move across the screen and stop at the 2+ hour mark. I also want the truck to turn from green to red overtime depending how long they have been waiting so we can prioritise easily.

    The first issue is, I can't work out the movement over time using lerp and have tried many different methods to solve this but to no avail.

    the second issue is, I can get the truck to turn from green to red over a time period but when I try to reset it, it fades to green then straight back to the colour it was at.

    i've attached a screenshot for visual aid.

    script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using DG.Tweening;
    6.  
    7. public class TruckInput : MonoBehaviour
    8. {
    9.  
    10.     public GameObject truck;
    11.  
    12.     public GameObject start;
    13.     public GameObject end;
    14.  
    15.     public string truckLoad;
    16.     public GameObject inputField;
    17.     public GameObject textDisplay;
    18.  
    19.     public GameObject startButton;
    20.     public GameObject resetButton;
    21.  
    22.     public Text timerText;
    23.  
    24.     private int seconds;
    25.     private int minutes;
    26.     private int hours;
    27.  
    28.     public bool startCou;
    29.  
    30.     public Material nMaterial;
    31.  
    32.     public void storeLoad()
    33.     {
    34.         truckLoad = inputField.GetComponent<Text>().text;
    35.         textDisplay.GetComponent<Text>().text = truckLoad;
    36.  
    37.         startButton.SetActive(false);
    38.         resetButton.SetActive(true);
    39.  
    40.         StartCoroutine("timeCounter");
    41.  
    42.     }
    43.  
    44.     public void eraseText()
    45.     {
    46.         textDisplay.GetComponent<Text>().text = " ";
    47.         startButton.SetActive(true);
    48.         resetButton.SetActive(false);
    49.  
    50.         seconds = 00;
    51.         minutes = 00;
    52.         hours = 00;
    53.  
    54.         timerText.text = "00:00:00";
    55.  
    56.  
    57.         StopCoroutine("timeCounter");
    58.  
    59.         StartCoroutine("truckReset");
    60.     }
    61.  
    62.     public IEnumerator timeCounter()
    63.     {
    64.         while (true)
    65.         {
    66.             yield return new WaitForSeconds(1);
    67.             seconds++;
    68.  
    69.             if (seconds == 60)
    70.             {
    71.                 minutes++;
    72.                 seconds = 00;
    73.             }
    74.  
    75.             if (minutes == 60)
    76.             {
    77.                 hours++;
    78.                 minutes = 00;
    79.             }
    80.          
    81.             timerText.text = hours.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString();
    82.  
    83.             //have truck move from start to end over 2 hours
    84.  
    85.             nMaterial.DOColor(Color.red, 7200);
    86.  
    87.         }
    88.     }
    89.  
    90.     public IEnumerator truckReset()
    91.     {
    92.         yield return new WaitForSeconds(1);
    93.  
    94.         nMaterial.DOColor(Color.green, 2);
    95.  
    96.         //reset truck back to start position in 2 seconds
    97.     }
    98. }
     

    Attached Files:

    Last edited: Aug 24, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    If you're struggling with Lerp it might be an idea to get something like DOTween or iTween (or any number of other free tweening) assets off the asset store and start there. No sense reinventing the wheel until you decide that existing wheels cannot do what you want to do.
     
  3. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Hello,

    Thank you for your response. Ive used DOTween already on the color change but I'm also getting it to actually move to and from point A and B. Also tried transform.movetowards and transform. Translate but they all have there own issues.

    Thanks
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Make blank GameObjects at the start and end of each trucks run and tween each truck between them.