Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class hitByMouse : MonoBehaviour {
  6.  
  7. public double destroyDistance = 4.0; // макс длина луча
  8. public Vector2 direction; //пустой вектор направления
  9.  
  10.  
  11. void Update () {
  12. Vector2 playerPos = gameObject.transform.position;
  13. Debug.Log(playerPos);
  14. //Debug.Log(direction);
  15. if (Input.GetMouseButtonDown(0)) //обрабатываем нажатие левой кнопки мыши
  16. {
  17.  
  18. Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); //переводим положение мыши в координату игрового мира
  19. RaycastHit2D hit = Physics2D.Raycast(playerPos, worldPoint); // объявлям луч от персонажа до положения мыши
  20. Debug.Log("distance " + hit.distance); // я оставил дебаг, что бы настроить длину луча от персонажа до объекта
  21. if (hit.collider != null) //проверка попадаем ли мы в объект с коллизией
  22. {
  23. if (hit.collider.tag == "block" && hit.distance <= destroyDistance) // если тег объекта равен нашему И дистанция подходит
  24. {
  25. Destroy(hit.collider.gameObject); //мы его уничтожаем
  26. }
  27. }
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement