Advertisement
uurha

Untitled

Apr 24th, 2022
1,445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TranslateObject_coroutines : MonoBehaviour
  6. {
  7.     [Header("Track Square")]
  8.     public Material matSquare;
  9.     public float TimeWaitSquare;
  10.     public float SpeedMoveSquare;
  11.  
  12.  
  13.  
  14.     [Header("Track Triangle")]
  15.     public Material matTriangle;
  16.     public float TimeWaitTiangle;
  17.     public float SpeedMoveTiangle;
  18.    
  19.  
  20.     [Header("Control Track form")]
  21.     public bool ChangeTrack;
  22.  
  23.    
  24.  
  25.     private void Start()
  26.     {
  27.        
  28.         //StartCoroutine(TrackIsScuare());
  29.         //StartCoroutine(TrackIsTriangle());
  30.  
  31.     }
  32.  
  33.  
  34.     private void Update()
  35.     {
  36.         /*
  37.         if (ChangeTrack == false)
  38.         {
  39.             StartCoroutine(TrackIsTriangle());
  40.             StopCoroutine(TrackIsScuare());
  41.  
  42.         }
  43.  
  44.         else
  45.         {
  46.             StartCoroutine(TrackIsScuare());
  47.             StopCoroutine(TrackIsTriangle());
  48.         }
  49.         */
  50.     }
  51.  
  52.  
  53.  
  54.  
  55.     private IEnumerator MoveFromTo(Vector3 startPosition, Vector3 endPosition, float time)
  56.     {
  57.         var currentTime = 0f;
  58.         while (currentTime < time)
  59.         {
  60.             transform.position = Vector3.Lerp(startPosition, endPosition, 1 - (time - currentTime) / time);
  61.             currentTime += Time.deltaTime;
  62.             yield return null;
  63.         }
  64.         transform.position = endPosition;
  65.     }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.     private IEnumerator TrackIsScuare()
  73.     {
  74.        
  75.         while (true)
  76.         {
  77.  
  78.             yield return MoveFromTo(new Vector3(0f, 1.6f, 0f), new Vector3(0f, 1.6f, 10f), SpeedMoveSquare);
  79.             GameObject obj1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  80.             obj1.transform.position = this.transform.position;
  81.             obj1.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  82.             obj1.GetComponent<Renderer>().material = matSquare;
  83.             yield return new WaitForSeconds(TimeWaitSquare);
  84.  
  85.             yield return MoveFromTo(new Vector3(0f, 1.6f, 10f), new Vector3(10f, 1.6f, 10f), SpeedMoveSquare);
  86.             GameObject obj2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  87.             obj2.transform.position = this.transform.position;
  88.             obj2.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  89.             obj2.GetComponent<Renderer>().material = matSquare;
  90.             yield return new WaitForSeconds(TimeWaitSquare);
  91.  
  92.             yield return MoveFromTo(new Vector3(10f, 1.6f, 10f), new Vector3(10f, 1.6f, 0f), SpeedMoveSquare);
  93.             GameObject obj3 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  94.             obj3.transform.position = this.transform.position;
  95.             obj3.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  96.             obj3.GetComponent<Renderer>().material = matSquare;
  97.             yield return new WaitForSeconds(TimeWaitSquare);
  98.  
  99.             yield return MoveFromTo(new Vector3(10f, 1.6f, 0f), new Vector3(0f, 1.6f, 0f), SpeedMoveSquare);
  100.             GameObject obj4 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  101.             obj4.transform.position = this.transform.position;
  102.             obj4.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  103.             obj4.GetComponent<Renderer>().material = matSquare;
  104.             yield return new WaitForSeconds(TimeWaitSquare);
  105.  
  106.  
  107.             /*Destroy(obj1);
  108.             Destroy(obj2);
  109.             Destroy(obj3);
  110.             Destroy(obj4);
  111.  
  112.             yield return null;
  113.             */
  114.  
  115.         }
  116.     }
  117.  
  118.  
  119.     private IEnumerator TrackIsTriangle()
  120.     {
  121.  
  122.         while (true)
  123.         {
  124.                 yield return MoveFromTo(new Vector3(0f, 1.6f, 0f), new Vector3(5f, 1.6f, 10f), SpeedMoveTiangle);
  125.                 GameObject obj1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  126.                 obj1.transform.position = this.transform.position;
  127.                 obj1.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  128.                 obj1.GetComponent<Renderer>().material = matTriangle;
  129.                 yield return new WaitForSeconds(TimeWaitTiangle);
  130.  
  131.                 yield return MoveFromTo(new Vector3(5f, 1.6f, 10f), new Vector3(10f, 1.6f, 0f), SpeedMoveTiangle);
  132.                 GameObject obj2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  133.                 obj2.transform.position = this.transform.position;
  134.                 obj2.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  135.                 obj2.GetComponent<Renderer>().material = matTriangle;
  136.                 yield return new WaitForSeconds(TimeWaitTiangle);
  137.  
  138.                 yield return MoveFromTo(new Vector3(10f, 1.6f, 0f), new Vector3(0f, 1.6f, 0f), SpeedMoveTiangle);
  139.                 GameObject obj3 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  140.                 obj3.transform.position = this.transform.position;
  141.                 obj3.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  142.                 obj3.GetComponent<Renderer>().material = matTriangle;
  143.                 yield return new WaitForSeconds(TimeWaitTiangle);
  144.         }
  145.        
  146.        
  147.  
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement