Advertisement
Guest User

SCRIPT

a guest
Feb 23rd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. public class EnemyTouch : MonoBehaviour
  2. {
  3.     public int currentScore;
  4.     public TextMeshProUGUI textScore;
  5.     Collider2D col;
  6.     public float minX;
  7.     public float maxX;
  8.     public float minY;
  9.     public float maxY;
  10.     Vector2 targetPosition;
  11.     public float baseSpeed = 0.7f;
  12.     public float speedBoost = 5.0f;
  13.     bool boosting;
  14.  
  15.     void Start()
  16.     {
  17.         col = GetComponent<Collider2D>();
  18.         targetPosition = GetRandomPosition();
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.         textScore.text = currentScore.ToString();
  25.         EnemyMovement();
  26.    
  27.        
  28.        
  29.     }
  30.     void EnemyMovement()
  31.     {
  32.  
  33.         /* if ((Vector2)transform.position != targetPosition && EnemyTouchAndScore()==true )
  34.          {
  35.              StartCoroutine(BoostSpeedCo());
  36.  
  37.  
  38.          }*/
  39.         //Si al menos se toca una vez la screen
  40.         if (Input.touchCount > 0)
  41.         {
  42.             Touch touch = Input.GetTouch(0);
  43.             Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
  44.             if (touch.phase == TouchPhase.Began)
  45.             {
  46.                 Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
  47.                 if (col == touchedCollider)
  48.                 {
  49.  
  50.                     currentScore += 1;
  51.                     // return true;
  52.  
  53.                 }
  54.             }
  55.  
  56.         }
  57.         if ((Vector2)transform.position != targetPosition)
  58.         {
  59.             transform.position = Vector2.MoveTowards(transform.position, targetPosition, baseSpeed * Time.deltaTime);
  60.         }
  61.         else
  62.         {
  63.             targetPosition = GetRandomPosition();
  64.         }
  65.     }
  66.     Vector2 GetRandomPosition()
  67.     {
  68.         float randomX = Random.Range(minX, maxX);
  69.         float randomY = Random.Range(minY, maxY);
  70.         return new Vector2(randomX, randomY);
  71.     }
  72.     IEnumerator BoostSpeedCo()
  73.     {
  74.         transform.position = Vector2.MoveTowards(transform.position, targetPosition, speedBoost * Time.deltaTime);
  75.         Debug.Log("boost");
  76.         yield return new WaitForSeconds(3.0f);
  77.         // transform.position = Vector2.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);      
  78.     }
  79.    
  80.  
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement