纳金网

标题: 被瞄准的对象显示血条或子物体(原理) [打印本页]

作者: 烟雨    时间: 2015-8-30 00:17
标题: 被瞄准的对象显示血条或子物体(原理)

如图 鼠标放在终点的cube显示出 Sphere;
164557gr0k6sh8y61rc76m.png
这个可以用来做瞄准物体显示血条,在端游中经常看见的功能
代码如下:
  1. using UnityEngine;
  2. using System.Collections;


  3. public class CubeSph : MonoBehaviour
  4. {
  5.     public GameObject sph;
  6.     // Use this for initialization
  7.     void Start()
  8.     {
  9.         sph = transform.Find("Sphere").gameObject;
  10.         sph.SetActive(false);
  11.     }


  12.     // Update is called once per frame
  13.     void Update()
  14.     {
  15.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  16.         RaycastHit hit;


  17.         if (Physics.Raycast(ray, out hit))
  18.         {
  19.             print("hit.name=" + hit.transform.tag);
  20.             if (hit.transform.tag == "enemy") //把cube的tag设置为  enemy
  21.             {
  22.                 sph = hit.transform.FindChild("Sphere").gameObject;
  23.                 sph.SetActive(true);
  24.             }         
  25.         }
  26.         else
  27.         {
  28.             print("没有");
  29.             sph.SetActive(false);
  30.         }


  31.     }
  32. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5