您的位置:首页 > 移动开发 > Unity3D

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

2015-06-25 17:09 381 查看
如图 鼠标放在终点的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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: