当前位置:网站首页>unity3d-射线
unity3d-射线
2022-07-13 18:02:00 【MaNongSa】
射线
struct in UnityEngine
1、描述
射线表示形式。
射线是从 origin 开始并按照某个 direction 行进的无限长的线。
2、变量
direction 射线的方向。
origin 射线的原点。
2.1、构造函数
Ray 沿着 direction 创建从 origin 开始的射线。
2.2、使用
- 代码
声名 RaycastHit 、Ray;//
RaycastHit hit;
Ray ray;
Ray((起点)(方向))//
ray = new Ray(Vector3.up,Vector3.forward);
如果有监测到就返回true否者返回false;//
if (Physics.Raycast(ray, out hit)){
打印碰到的信息//
Debug.DrawRay(ray.origin, ray.direction * 20, Color.red);
Debug.DrawLine(ray.origin,ray.direction*20,Color.yellow);
Debug.Log(hit.collider.gameObject);
Debug.Log(hit.collider.tag);
Debug.Log(hit.point);
Debug.Log(hit.collider.transform.position);
Debug.Log(hit.collider.name);
}
3、小案例
- 案例说明
- 通过鼠标点击实现发射球去打东西
//声名
RaycastHit hit;
Ray ray;
public GameObject attack;
public float speed = 5;
//监测判断
if (Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit))
{
画线测试//
Debug.DrawLine(transform.position, hit.point, Color.red);
创建小球
GameObject a=Instantiate(attack, transform.position, Quaternion.identity);
a.GetComponent<Rigidbody>().velocity = (hit.point - transform.position) * speed;
}
}
边栏推荐
- LeetCode 刷题第十三天 + DL第三天
- Chapter 4 stm32+ld3320+syn6288+dht11 realize voice acquisition of temperature and humidity values (Part 2)
- 硬件课程设计:基于STM32的多功能播放器之聊天功能
- Binary search method
- Go seckill system 1 -- Erlang environment installation.
- Rttread dynamic memory allocation
- Use of rounding, rounding down, rounding up
- Leetcode-1 sum of two numbers (STL, hashtable, unordered_map)
- Go秒杀系统3--Work模式,Publish模式
- unity3d-常用组件
猜你喜欢
随机推荐
unity3d-常用组件
Database dark horse notes DDL
双向链表List类模板的实现
动态数组Vector类模板的实现
LeetCode 第十八天
字节一面挂了,面试官问我DDD,我却不知道
Leetcode-3 longest string without repeated characters (sliding window, unordered_set, st.find (string[i])
Airplane games
KMP中的最小循环节
Gobang (2)
DDD——在我梦里,我还能让你把我给欺负了?
最大相连子序列和
Briefly, the process
男人要“强”,不要“软”“弱”,“虚”得一匹
物联网学习之旅——初始
CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
Gobang (1)
Experience of using voice chip jq8400
Dynamic changes based on arrays and nodes (addition, deletion, modification and query)
Talk about the interface









