Advertisement
CyberN00b

Untitled

Oct 2nd, 2022 (edited)
829
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5.  
  6. public class Moving : MonoBehaviour
  7. {
  8.     public int _coinCount = 0;
  9.     private Queue<Vector2> _queuePos;
  10.     [SerializeField]
  11.     private float _speed = 5;
  12.     private bool _moving = false;
  13.     private Vector2 _destination;
  14.     void Start()
  15.     {
  16.  
  17.     }
  18.  
  19.     void Update()
  20.     {
  21.  
  22.         if(Input.touchCount > 0)
  23.         {
  24.             Touch touch = Input.GetTouch(0);
  25.             _queuePos.Enqueue(touch.position);
  26.         }
  27.         if (!_moving &&_queuePos.Count > 0)
  28.             StartCoroutine(LerpPosition(_queuePos.Dequeue()))
  29.     }
  30.  
  31.     IEnumerator LerpPosition(Vector2 target)
  32.     {
  33.         _moving = true;
  34.         float distance = Vector2.Distance(transform.position, target), passedDist = 0;
  35.         while (passedDist < distance)
  36.         {
  37.             transform.position = Vector2.Lerp(transform.position, target, _speed * Time.deltaTime);
  38.             passedDist += _speed * Time.deltaTime;
  39.             yield return null;
  40.         }
  41.         transform.position = target;
  42.         _moving = false;
  43.     }
  44.  
  45.     private void OnTriggerEnter2D(Collider2D collision)
  46.     {
  47.         if(collision.name == "Spike")
  48.             Destroy(gameObject);
  49.         if (collision.name == "Coin")
  50.         {
  51.             _coinCount += 1;
  52.             Destroy(collision.gameObject);
  53.         }
  54.  
  55.     }
  56. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement