Advertisement
LeeMace

Swiping (Fruit Ninja Style)

Dec 12th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(TrailRenderer), typeof(BoxCollider))]
  5.  
  6. public class ClickAndSwipe : MonoBehaviour
  7. {
  8.     private GameManager gameManager;
  9.     private Camera cam;
  10.     private Vector3 mousePos;
  11.     private TrailRenderer trail;
  12.     private BoxCollider col;
  13.     private bool swiping = false;
  14.  
  15.     // Start is called before the first frame update
  16.     void Awake()
  17.     {
  18.         cam = Camera.main;
  19.         trail = GetComponent<TrailRenderer>();
  20.         col = GetComponent<BoxCollider>();
  21.         trail.enabled = false;
  22.         col.enabled = false;
  23.  
  24.         gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
  25.  
  26.     }
  27.  
  28.     // Update is called once per frame
  29.     void Update()
  30.     {
  31.         if (gameManager.isGameActive)
  32.         {
  33.             if(Input.GetMouseButtonDown(0))
  34.             {
  35.                 swiping = true;
  36.                 UpdateComponents();
  37.             }
  38.             else if (Input.GetMouseButtonUp(0))
  39.             {
  40.                 swiping = false;
  41.                 UpdateComponents();
  42.             }
  43.  
  44.             if (swiping)
  45.             {
  46.                 UpdateMousePosition();
  47.             }
  48.         }
  49.     }
  50.  
  51.     void UpdateMousePosition()
  52.     {
  53.         mousePos = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));
  54.         transform.position = mousePos;
  55.     }
  56.  
  57.     void UpdateComponents()
  58.     {
  59.         trail.enabled = swiping;
  60.         col.enabled = swiping;
  61.  
  62.     }
  63.  
  64.     private void OnCollisionEnter(Collision collision)
  65.     {
  66.         if(collision.gameObject.GetComponent<Targets>())
  67.         {
  68.             collision.gameObject.GetComponent<Targets>().DestroyTarget();
  69.         }
  70.     }
  71. }
  72.  
  73. //in the targets script add this and comment out this
  74.  
  75.  /* private void OnMouseDown()
  76.     {
  77.         if (gameManager.isGameActive)
  78.         {
  79.             Destroy(gameObject);
  80.             gameManager.UpdateScore(pointValue);
  81.             Instantiate(explosiveParticle, transform.position, explosiveParticle.transform.rotation);
  82.         }
  83.     }*/
  84.     // mouse click destroys object and adds score and does particles
  85.     public void DestroyTarget()
  86.     {
  87.         if (gameManager.isGameActive)
  88.         {
  89.             Destroy(gameObject);
  90.             gameManager.UpdateScore(pointValue);
  91.             Instantiate(explosiveParticle, transform.position, explosiveParticle.transform.rotation);
  92.         }
  93.  
  94.     }
Tags: swiping
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement