Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TranslateObject_coroutines : MonoBehaviour
- {
- [Header("Track Square")]
- public Material matSquare;
- public float TimeWaitSquare;
- public float SpeedMoveSquare;
- [Header("Track Triangle")]
- public Material matTriangle;
- public float TimeWaitTiangle;
- public float SpeedMoveTiangle;
- [Header("Control Track form")]
- public bool ChangeTrack;
- private void Start()
- {
- //StartCoroutine(TrackIsScuare());
- //StartCoroutine(TrackIsTriangle());
- }
- private void Update()
- {
- /*
- if (ChangeTrack == false)
- {
- StartCoroutine(TrackIsTriangle());
- StopCoroutine(TrackIsScuare());
- }
- else
- {
- StartCoroutine(TrackIsScuare());
- StopCoroutine(TrackIsTriangle());
- }
- */
- }
- private IEnumerator MoveFromTo(Vector3 startPosition, Vector3 endPosition, float time)
- {
- var currentTime = 0f;
- while (currentTime < time)
- {
- transform.position = Vector3.Lerp(startPosition, endPosition, 1 - (time - currentTime) / time);
- currentTime += Time.deltaTime;
- yield return null;
- }
- transform.position = endPosition;
- }
- private IEnumerator TrackIsScuare()
- {
- while (true)
- {
- yield return MoveFromTo(new Vector3(0f, 1.6f, 0f), new Vector3(0f, 1.6f, 10f), SpeedMoveSquare);
- GameObject obj1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
- obj1.transform.position = this.transform.position;
- obj1.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
- obj1.GetComponent<Renderer>().material = matSquare;
- yield return new WaitForSeconds(TimeWaitSquare);
- yield return MoveFromTo(new Vector3(0f, 1.6f, 10f), new Vector3(10f, 1.6f, 10f), SpeedMoveSquare);
- GameObject obj2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
- obj2.transform.position = this.transform.position;
- obj2.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
- obj2.GetComponent<Renderer>().material = matSquare;
- yield return new WaitForSeconds(TimeWaitSquare);
- yield return MoveFromTo(new Vector3(10f, 1.6f, 10f), new Vector3(10f, 1.6f, 0f), SpeedMoveSquare);
- GameObject obj3 = GameObject.CreatePrimitive(PrimitiveType.Cube);
- obj3.transform.position = this.transform.position;
- obj3.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
- obj3.GetComponent<Renderer>().material = matSquare;
- yield return new WaitForSeconds(TimeWaitSquare);
- yield return MoveFromTo(new Vector3(10f, 1.6f, 0f), new Vector3(0f, 1.6f, 0f), SpeedMoveSquare);
- GameObject obj4 = GameObject.CreatePrimitive(PrimitiveType.Cube);
- obj4.transform.position = this.transform.position;
- obj4.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
- obj4.GetComponent<Renderer>().material = matSquare;
- yield return new WaitForSeconds(TimeWaitSquare);
- /*Destroy(obj1);
- Destroy(obj2);
- Destroy(obj3);
- Destroy(obj4);
- yield return null;
- */
- }
- }
- private IEnumerator TrackIsTriangle()
- {
- while (true)
- {
- yield return MoveFromTo(new Vector3(0f, 1.6f, 0f), new Vector3(5f, 1.6f, 10f), SpeedMoveTiangle);
- GameObject obj1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- obj1.transform.position = this.transform.position;
- obj1.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
- obj1.GetComponent<Renderer>().material = matTriangle;
- yield return new WaitForSeconds(TimeWaitTiangle);
- yield return MoveFromTo(new Vector3(5f, 1.6f, 10f), new Vector3(10f, 1.6f, 0f), SpeedMoveTiangle);
- GameObject obj2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- obj2.transform.position = this.transform.position;
- obj2.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
- obj2.GetComponent<Renderer>().material = matTriangle;
- yield return new WaitForSeconds(TimeWaitTiangle);
- yield return MoveFromTo(new Vector3(10f, 1.6f, 0f), new Vector3(0f, 1.6f, 0f), SpeedMoveTiangle);
- GameObject obj3 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- obj3.transform.position = this.transform.position;
- obj3.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
- obj3.GetComponent<Renderer>().material = matTriangle;
- yield return new WaitForSeconds(TimeWaitTiangle);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement