unity ray和line射线检测
RaycastHit 光线投射碰撞
Struct
Structure used to get information back from a raycast.
用来获取从raycast函数中得到的信息反馈的结构。
参见:Physics.Raycast, Physics.Linecast, Physics.RaycastAll.
Variables变量
-
The impact point in world space where the ray hit the collider.
在世界空间中,射线碰到碰撞器的碰撞点。 -
The normal of the surface the ray hit.
射线所碰到的表面的法线。 -
The barycentric coordinate of the triangle we hit.
所碰到的三角形的重心坐标。 -
The distance from the ray's origin to the impact point.
从光线的原点到碰撞点的距离。 -
The index of the triangle that was hit.
碰到的三角形的索引。 -
The uv texture coordinate at the impact point.
在碰撞点的UV纹理坐标。 -
The secondary uv texture coordinate at the impact point.
碰撞点的第二个UV纹理坐标。 -
The uv lightmap coordinate at the impact point.
所在碰撞点的光照图UV坐标。 -
The Collider that was hit.
碰到的碰撞器。 -
The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.
碰到的碰撞器的Rigidbody。如果该碰撞器没有附加刚体那么它为null。 -
The Transform of the rigidbody or collider that was hit.
碰到的刚体或碰撞器的变换。
Physics.Raycast 光线投射
static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters参数
- originThe starting point of the ray in world coordinates.
在世界坐标,射线的起始点。 - directionThe direction of the ray.
射线的方向。 - distanceThe length of the ray
射线的长度。 - layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.
只选定Layermask层内的碰撞器,其它层内碰撞器忽略。
Returns
bool - True when the ray intersects any collider, otherwise false.
当光线投射与任何碰撞器交叉时为真,否则为假。
Description描述
Casts a ray against all colliders in the scene.
在场景中投下可与所有碰撞器碰撞的一条光线。
- using UnityEngine;
- using System.Collections;
- public class Example : MonoBehaviour {
- void Update() {
- Vector3 fwd = transform.TransformDirection(Vector3.forward);
- if (Physics.Raycast(transform.position, fwd, 10))
- print("There is something in front of the object!");
- }
- }
Note: This function will return false if you cast a ray from inside a sphere to the outside; this in an intended behaviour.
注意:如果从一个球型体的内部到外部用光线投射,返回为假。
• static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters参数
- originThe starting point of the ray in world coordinates.
在世界坐标,射线的起始点。 - directionThe direction of the ray.
射线的方向。 - distanceThe length of the ray
射线的长度。 - hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
如果返回true,hitInfo将包含碰到器碰撞的更多信息。 - layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.
只选定Layermask层内的碰撞器,其它层内碰撞器忽略。
Returns
bool - True when the ray intersects any collider, otherwise false.
当光线投射与任何碰撞器交叉时为真,否则为假。
Description描述
Casts a ray against all colliders in the scene and returns detailed information on what was hit.
在场景中投下可与所有碰撞器碰撞的一条光线,并返回碰撞的细节信息。
- using UnityEngine;
- using System.Collections;
- public class Example : MonoBehaviour {
- void Update() {
- RaycastHit hit;
- if (Physics.Raycast(transform.position, -Vector3.up, out hit))
- float distanceToGround = hit.distance;
- }
- }
又一个例子:
- using UnityEngine;
- using System.Collections;
- public class Example : MonoBehaviour {
- void Update() {
- RaycastHit hit;
- if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0F))
- float distanceToGround = hit.distance;
- }
- }
• static function Raycast (ray : Ray, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters参数
- rayThe starting point and direction of the ray.
射线的起点和方向 - distanceThe length of the ray
射线的长度。 - layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.
只选定Layermask层内的碰撞器,其它层内碰撞器忽略。
Returns
bool - True when the ray intersects any collider, otherwise false.
当光线投射与任何碰撞器交叉时为真,否则为假。
Description描述
Same as above using ray.origin and ray.direction instead of origin and direction.
使用ray.origin和ray.direction同上,替代origin和direction。
- using UnityEngine;
- using System.Collections;
- public class Example : MonoBehaviour {
- void Update() {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, 100))
- print("Hit something");
- }
- }
• static function Raycast (ray : Ray, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
Parameters参数
- rayThe starting point and direction of the ray.
射线的起点和方向 - distanceThe length of the ray
射线的长度 - hitInfoIf true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
如果返回true,hitInfo将包含碰到器碰撞的更多信息。 - layerMaskA Layer mask that is used to selectively ignore colliders when casting a ray.
只选定Layermask层内的碰撞器,其它层内碰撞器忽略。
Returns
bool - True when the ray intersects any collider, otherwise false.
当光线投射与任何碰撞器交叉时为真,否则为假。
Description描述
Same as above using ray.origin and ray.direction instead of origin and direction.
使用ray.origin和ray.direction同上,替代origin和direction。
- using UnityEngine;
- using System.Collections;
- public class Example : MonoBehaviour {
- void Update() {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, 100))
- Debug.DrawLine(ray.origin, hit.point);
- }
- }
Mathf.Infinity 正无穷
static var Infinity : float
Description描述
A representation of negative infinity (Read Only).
表示正无穷,也就是无穷大,∞ (只读)[in'finiti]
- // Casts a ray from (0,0,0) towards (0,0,1) to the infinity and prints a message
- //画一条射线从(0,0,0) 向 (0,0,1)到无穷远,并打印一个消息
- // if any object has touched the ray.
- //如果任意物体碰到这个射线
- // To test it, just place any object and intersect it with the white drawn line
- //为便于测试,只需放置任意物体相交于白色的绘制线
- function Update () {
- // shows the line that follows the ray.
- //显示这条射线
- print ("There is something in front of the object!");
- }
- }
Ray 射线
Struct
Representation of rays.
表示射线。
A ray is an infinite line starting at origin and going in some direction.
射线是一个无穷的线,开始于origin并沿着direction方向。
Variables变量
-
The origin point of the ray.
射线的起点 -
The direction of the ray.
射线的方向
Constructors构造器
Functions函数
-
Returns a point at distance units along the ray.
返回沿着射线在distance距离单位的点。 -
Returns a nicely formatted string for this ray.
返回该光线格式化好的字符串
Ray.GetPoint 获取点
function GetPoint (distance : float) : Vector3
Description描述
Returns a point at distance units along the ray.
返回沿着射线在distance距离单位的点。
- using UnityEngine;
- using System.Collections;
- public class example : MonoBehaviour {
- public Ray r;
- public void Awake() {
- print(r.GetPoint(10));
- }
- }
Ray.ToString 转字符串
function ToString () : string
function ToString (format : string) : string
Description描述
Returns a nicely formatted string for this ray.
返回该光线格式化好的字符串。
Physics.Linecast 线性投射
static function Linecast (start : Vector3, end : Vector3, layerMask : int = kDefaultRaycastLayers) : bool
Description描述
Returns true if there is any collider intersecting the line between start and end.
从开始位置到结束位置做一个光线投射,如果与碰撞体交互,返回真。
- using UnityEngine;
- using System.Collections;
- public class example : MonoBehaviour {
- public Transform target;
- void Update() {
- if (!Physics.Linecast(transform.position, target.position))
- ProcessData.AndDoSomeCalculations();
- }
- }
Layer mask is used to selectively ignore colliders when casting a ray.
可以根据Layer mask层的不同来忽略碰撞体。
• static function Linecast (start : Vector3, end : Vector3, out hitInfo : RaycastHit, layerMask : int = kDefaultRaycastLayers) : bool
Description描述
Returns true if there is any collider intersecting the line between start and end.
从开始位置到结束位置做一个光线投射,如果与碰撞体交互,返回真。
If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit). Layer mask is used to selectively ignore colliders when casting a ray.
如果交互到碰撞体,光线投射返回一个RaycastHit结构体信息。可以根据Layer mask层的不同来忽略碰撞体。
Physics.RaycastAll 所有光线投射
static function RaycastAll (ray : Ray, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : RaycastHit[]
static function RaycastAll (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layermask : int = kDefaultRaycastLayers) : RaycastHit[]
Description描述
Casts a ray through the scene and returns all hits.
投射一条光线并返回所有碰撞,也就是投射光线并返回一个RaycastHit[]结构体。
- function Update () {
- var hits : RaycastHit[];
- hits = Physics.RaycastAll (transform.position, transform.forward, 100.0);
- // Change the material of all hit colliders
- // to use a transparent Shader
- //改变所有碰到碰撞器的材质,使用透明的着色器
- for (var i = 0;i < hits.Length; i++) {
- var hit : RaycastHit = hits[i];
- var renderer = hit.collider.renderer;
- if (renderer) {
- renderer.material.shader = Shader.Find("Transparent/Diffuse");
- renderer.material.color.a = 0.3;
- }
- }
- }
Note: This function will return false if you cast a ray from inside a sphere to the outside; this in an intended behaviour.
注意:如果从一个球型体的内部到外部用光线投射,返回错误。
Physics.Raycast 为投射射线的碰撞检测,长度可为Infinity(无穷大),返回值为一个bool值
Physics.Linecast 投射的是一条线段,有开始值和结束值,返回值也为bool
Physics.RaycastAll 投射的是一条射线,但返回值为RancastHit的一个结构体
c
d
unity ray和line射线检测的更多相关文章
- Unity - Raycast 射线检测
本文简要分析了Unity中射线检测的基本原理及用法,包括: Ray 射线 RaycastHit 光线投射碰撞信息 Raycast 光线投射 SphereCast 球体投射 OverlapSphere ...
- Unity射线检测的用法总结
RayCast 射线检测 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心 ...
- unity射线检测
unity中射线检测时非常实用也经常实用的一种手段.下面讲解一下射线检测问题. 1)Ray 根据射线端点和射线的方向定义一条射线 Ray ray= new Ray(transform.position ...
- 日常小节----unity小坑记(射线检测固定层级)
unity中射线检测需设定所需层级时,必须加上距离!!! //一条从主相机到屏幕点击点的射线 Ray ray = Camera.Main.ScreenPointToRay(Input.mousePos ...
- Unity——射线检测(鼠标点击开关门效果)
Unity射线检测--实现简单的开关门效果 简要:通过鼠标点击来发射一条射线,来获得射线所碰到的物体名称,再通过改变门的Rotation值来实现开关门的效果. 一.代码实现 1.1 简易的场景搭建 注 ...
- Unity的学习笔记(射线检测)
首先,射线检测的API是这样的,网上找了一下,这个图片看得很清楚: 接下来是自己使用这个进行测试 using System.Collections; using System.Collections. ...
- Ray射线检测和Recources.Load
记录射线检测常用的方法,以及Rocources.Load的常用用法 使用代码实现鼠标点击在鼠标点击处生成制定gameObject RayCastHit hit; void Update() { Ray ...
- pico g2 触摸板手柄射线检测---for unity
1.pico g2手柄射线检测UI,需要在canvas添加Graphic Raycaster脚本和Pvr_Ui Canvas脚本. 2.删除掉原有的maincamera,将Pvr_unitySDK下h ...
- Unity3D_(API)射线检测Raycast()
Unity射线检测官方文档: 传送门 一.检测前方是否有游戏物体(射线无限长度) 二.检测前方是否有游戏物体(射线长度为1m) 三.检测前方游戏物体碰撞信息(射线无限长度): 四.指定检测碰撞Tag层 ...
随机推荐
- 利用MVC Chart 打造后台图表、前端图表
应用场景是这个样子的:要做导出数据到PDF的功能,涉及到文本.表格.图表等内容.在做图表功能时,发现之前用Highcharts做的图表根本就不能集成到PDF中.这里需要一个能在程序后台就生成图表的功能 ...
- python + pyqt5 QlineEdit QMessageBox实现信息录入和消息弹框提醒
本人现在在做自动化工具开发的工作,因此,记录下自己平时遇到的问题和解决之道,还有一些简单的小工具 以下为主代码 # --*-- coding:utf-8 --*-- from first import ...
- 乘风破浪:LeetCode真题_035_Search Insert Position
乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...
- OWASP TOP10(2017)
原文链接:https://www.t00ls.net/viewthread.php?from=notice&tid=39385
- spark-机器学习实践-K近邻应用实践一
K近邻应用-异常检测应用 原理: 根据数据样本进行KMeans机器学习模型的建立,获取簇心点,以簇为单位,离簇心最远的第五个点的距离为阈值,大于这个值的为异常点,即获得数据异常. 如图:
- 解决ThinkPHP中开启调试模式无法加载模块的问题。
刚开始学习ThinkPHP就遇到这种问题,还是自己粗心. 错误如下: 原因:开启调试模式,区分大小写的,要把模块名首字母大写就OK了.也就是: [plain] view plain copy http ...
- Spring boot整合Hive
使用Spring boot整合Hive,在启动Spring boot项目时,报出异常: java.lang.NoSuchMethodError: org.eclipse.jetty.servlet.S ...
- ArcMap 导入Excel坐标数据
1 准备Excel坐标数据集合 2 ArcMap加入Excel数据 将excel文件放入arcmap工作区的物理路径下 在工作区的根图层上点键,选择添加数据,找到excel文件并选择相应的工作薄 ...
- 【[AHOI2013]差异】
这个题一看就是为后缀家族设计的 我们看到我们要求的这个柿子 \[\sum_{i=1}^n\sum_{j=i+1}^nT_i+T_j-2\times lcp(T_i,T_j)\] 显然的是前面的那些东西 ...
- Spark项目之电商用户行为分析大数据平台之(十)IDEA项目搭建及工具类介绍
一.创建Maven项目 创建项目,名称为LogAnalysis 二.常用工具类 2.1 配置管理组建 ConfigurationManager.java import java.io.InputStr ...