Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class hitByMouse : MonoBehaviour {
- public double destroyDistance = 4.0; // макс длина луча
- public Vector2 direction; //пустой вектор направления
- void Update () {
- Vector2 playerPos = gameObject.transform.position;
- Debug.Log(playerPos);
- //Debug.Log(direction);
- if (Input.GetMouseButtonDown(0)) //обрабатываем нажатие левой кнопки мыши
- {
- Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); //переводим положение мыши в координату игрового мира
- RaycastHit2D hit = Physics2D.Raycast(playerPos, worldPoint); // объявлям луч от персонажа до положения мыши
- Debug.Log("distance " + hit.distance); // я оставил дебаг, что бы настроить длину луча от персонажа до объекта
- if (hit.collider != null) //проверка попадаем ли мы в объект с коллизией
- {
- if (hit.collider.tag == "block" && hit.distance <= destroyDistance) // если тег объекта равен нашему И дистанция подходит
- {
- Destroy(hit.collider.gameObject); //мы его уничтожаем
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement