【Unity学习笔记】——射线应用,光点随鼠标移动
2017-07-27 16:16
579 查看
实现一个随鼠标移动的光点,光点指到特定物体上会显示,否则隐藏
①使用tag
②使用Layer
方法①
用target标签标记场景物体
创建一个Image UI
在相机上添加下列脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayUI : MonoBehaviour {
private Ray ray;
private RaycastHit hit;//射线碰到的碰撞信息
public RectTransform UI;//准心
private void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.collider.tag == "target")
{
UI.gameObject.SetActive(true);
UI.position = Input.mousePosition;
}
else
UI.gameObject.SetActive(false);
}
else
UI.gameObject.SetActive(false);
}
}
方法②
用target层标记场景物体
在相机上添加下列脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayUI : MonoBehaviour {
private Ray ray;
private RaycastHit hit;//射线碰到的碰撞信息
public RectTransform UI;//准心
public LayerMask myLayer;
private void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100, myLayer.value))
{
//if (hit.collider.tag == "target")
//{
UI.gameObject.SetActive(true);
UI.position = Input.mousePosition;
//}
//else
// UI.gameObject.SetActive(false);
}
else
UI.gameObject.SetActive(false);
}
}
①使用tag
②使用Layer
方法①
用target标签标记场景物体
创建一个Image UI
在相机上添加下列脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayUI : MonoBehaviour {
private Ray ray;
private RaycastHit hit;//射线碰到的碰撞信息
public RectTransform UI;//准心
private void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.collider.tag == "target")
{
UI.gameObject.SetActive(true);
UI.position = Input.mousePosition;
}
else
UI.gameObject.SetActive(false);
}
else
UI.gameObject.SetActive(false);
}
}
方法②
用target层标记场景物体
在相机上添加下列脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayUI : MonoBehaviour {
private Ray ray;
private RaycastHit hit;//射线碰到的碰撞信息
public RectTransform UI;//准心
public LayerMask myLayer;
private void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100, myLayer.value))
{
//if (hit.collider.tag == "target")
//{
UI.gameObject.SetActive(true);
UI.position = Input.mousePosition;
//}
//else
// UI.gameObject.SetActive(false);
}
else
UI.gameObject.SetActive(false);
}
}
相关文章推荐
- Unity学习笔记——鼠标移动到物品上显示物品名字,点击后显示物品信息
- 学习笔记:架构 移动应用架构 view层的组织和调用方案(二)
- 【Unity 3D】学习笔记四十:射线
- KING_UNITY学习之鼠标点击地面控制人物移动
- 学习笔记:架构 移动应用架构 网络层设计方案(三)
- Unity学习2 鼠标点击控制物体移动
- Unity学习笔记 1st —— 物体会移动就是游戏的开始!
- 【Unity 3D】学习笔记四十:射线
- 蓝鸥Unity入门鼠标事件学习笔记
- unity camera 移动学习笔记
- HTML学习笔记——如何让图片随鼠标移动
- android移动应用开发学习笔记(三)——用户信息提交
- Unity Application Block 学习笔记 之 装饰模式应用
- SMP3.0学习笔记之八 使用Kapsel Update插件实现移动应用的自动更新
- Android移动应用开发学习笔记(二)——用户注册
- 【unity学习笔记】手机RPG游戏中角色移动方式实现
- android移动应用开发学习笔记(五)——模拟qq登陆界面
- OSG学习笔记16 - 对点选物体平移(鼠标点选物体)(物体随鼠标移动)(屏幕坐标转世界坐标)
- Ferris教程学习笔记:js示例5.6 跟随鼠标移动(大图展示)
- Unity学习笔记-2D横版游戏中的背景移动