Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(TrailRenderer), typeof(BoxCollider))]
- public class ClickAndSwipe : MonoBehaviour
- {
- private GameManager gameManager;
- private Camera cam;
- private Vector3 mousePos;
- private TrailRenderer trail;
- private BoxCollider col;
- private bool swiping = false;
- // Start is called before the first frame update
- void Awake()
- {
- cam = Camera.main;
- trail = GetComponent<TrailRenderer>();
- col = GetComponent<BoxCollider>();
- trail.enabled = false;
- col.enabled = false;
- gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
- }
- // Update is called once per frame
- void Update()
- {
- if (gameManager.isGameActive)
- {
- if(Input.GetMouseButtonDown(0))
- {
- swiping = true;
- UpdateComponents();
- }
- else if (Input.GetMouseButtonUp(0))
- {
- swiping = false;
- UpdateComponents();
- }
- if (swiping)
- {
- UpdateMousePosition();
- }
- }
- }
- void UpdateMousePosition()
- {
- mousePos = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));
- transform.position = mousePos;
- }
- void UpdateComponents()
- {
- trail.enabled = swiping;
- col.enabled = swiping;
- }
- private void OnCollisionEnter(Collision collision)
- {
- if(collision.gameObject.GetComponent<Targets>())
- {
- collision.gameObject.GetComponent<Targets>().DestroyTarget();
- }
- }
- }
- //in the targets script add this and comment out this
- /* private void OnMouseDown()
- {
- if (gameManager.isGameActive)
- {
- Destroy(gameObject);
- gameManager.UpdateScore(pointValue);
- Instantiate(explosiveParticle, transform.position, explosiveParticle.transform.rotation);
- }
- }*/
- // mouse click destroys object and adds score and does particles
- public void DestroyTarget()
- {
- if (gameManager.isGameActive)
- {
- Destroy(gameObject);
- gameManager.UpdateScore(pointValue);
- Instantiate(explosiveParticle, transform.position, explosiveParticle.transform.rotation);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement