unity中被瞄准的对象显示血条或子物体(原理)

来源:互联网 发布:域名注销查询 编辑:程序博客网 时间:2024/05/16 16:10

如图 鼠标放在终点的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);
        }


    }
}


蛮牛博客:http://www.unitymanual.com/home.php?mod=space&uid=8069&do=blog&view=me

0 0
原创粉丝点击