如图 鼠标放在终点的cube显示出 Sphere; 这个可以用来做瞄准物体显示血条,在端游中经常看见的功能 代码如下: - using UnityEngine;
- using System.Collections;
- public class CubeSph : MonoBehaviour
- {
- public GameObject sph;
- // Use this for initialization
- void Start()
- {
- sph = transform.Find("Sphere").gameObject;
- sph.SetActive(false);
- }
- // Update is called once per frame
- void Update()
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit))
- {
- print("hit.name=" + hit.transform.tag);
- if (hit.transform.tag == "enemy") //把cube的tag设置为 enemy
- {
- sph = hit.transform.FindChild("Sphere").gameObject;
- sph.SetActive(true);
- }
- }
- else
- {
- print("没有");
- sph.SetActive(false);
- }
- }
- }
复制代码 |