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
这个可以用来做瞄准物体显示血条,在端游中经常看见的功能
代码如下:
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
相关文章推荐
- Unity学习疑问记录之查找
- Unity3D 内存管理
- 【Unity实用小技巧】NGUI插件的使用总结(一)
- Unity3D占用内存太大的解决方法
- Unity中的对象池用法
- Unity3D中删除组件的方式
- unity 在移动平台中,文件操作路径详解
- Unity3d中制作Loading场景进度条所遇到的问题
- Unity5中的高性能物理组件
- 为ubuntu安装主题管理工具unity tweak tool
- 【Unity基础知识之一】 Unity支持 IOS 64-BIT
- unity发布后运行的错行日志总结
- Unity3D实践系列08, MonoBehaviour类的各种触发事件
- unity开发相关环境(vs、MonoDevelop)windows平台编码问题
- unity, Global和Local编辑模式
- Unity3d《Shader篇》法线贴图
- Unity开发优化----删除多余的MeshCollider和Animation组件
- unity 引入 android第三方sdk
- unity调用Android的jar包
- Unity 3D UV实现小地图