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.
-
//顯示這條射線
-
-
if (Physics.Raycast (Vector3.zero, Vector3.forward, Mathf.Infinity)) {
-
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