Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- public class Moving : MonoBehaviour
- {
- public int _coinCount = 0;
- private Queue<Vector2> _queuePos;
- [SerializeField]
- private float _speed = 5;
- private bool _moving = false;
- private Vector2 _destination;
- void Start()
- {
- }
- void Update()
- {
- if(Input.touchCount > 0)
- {
- Touch touch = Input.GetTouch(0);
- _queuePos.Enqueue(touch.position);
- }
- if (!_moving &&_queuePos.Count > 0)
- StartCoroutine(LerpPosition(_queuePos.Dequeue()))
- }
- IEnumerator LerpPosition(Vector2 target)
- {
- _moving = true;
- float distance = Vector2.Distance(transform.position, target), passedDist = 0;
- while (passedDist < distance)
- {
- transform.position = Vector2.Lerp(transform.position, target, _speed * Time.deltaTime);
- passedDist += _speed * Time.deltaTime;
- yield return null;
- }
- transform.position = target;
- _moving = false;
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if(collision.name == "Spike")
- Destroy(gameObject);
- if (collision.name == "Coin")
- {
- _coinCount += 1;
- Destroy(collision.gameObject);
- }
- }
- }
Advertisement
Advertisement