Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Map : MonoBehaviour
- {
- public Camera mapCamera;
- public Text objectWasHit;
- public GameObject mouseOvered;
- public float distanceToHit = 1000;
- private Camera[] cameras;
- private List<GameObject> objectsToHit = new List<GameObject>();
- // Use this for initialization
- void Start()
- {
- cameras = Camera.allCameras;
- GameObject levels = GameObject.Find("_Level");
- foreach (Transform child in levels.transform)
- {
- child.gameObject.AddComponent<UnityEngine.MeshRenderer>();
- objectsToHit.Add(child.gameObject);
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.M))
- {
- if (mapCamera.enabled == false)
- {
- foreach (Camera cam in cameras)
- {
- cam.enabled = false;
- }
- mapCamera.enabled = true;
- }
- else
- {
- foreach (Camera cam in cameras)
- {
- cam.enabled = true;
- }
- mapCamera.enabled = false;
- }
- }
- bool rcHit = false;
- Vector3 mouse = Input.mousePosition;
- Ray castPoint = mapCamera.ScreenPointToRay(mouse);
- RaycastHit hit;
- Debug.DrawRay(castPoint.origin, castPoint.direction * distanceToHit, Color.magenta);
- if (Physics.Raycast(castPoint, out hit, distanceToHit))
- {
- rcHit = true;
- if (mouseOvered != hit.collider.gameObject)
- {
- mouseOvered = hit.collider.gameObject;
- }
- objectWasHit.text = mouseOvered.name;
- }
- if (!rcHit && mouseOvered != null)
- {
- mouseOvered = null;
- }
- }
- }
- objectWasHit.text = mouseOvered.name;
Add Comment
Please, Sign In to add comment