赞
踩
Structure used to get information back from a raycast.
用来获取从raycast函数中得到的信息反馈的结构。
参见:Physics.Raycast, Physics.Linecast, Physics.RaycastAll.
static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
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
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
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
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);
-
- }
- }
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.
- //显示这条射线
- Debug.DrawLine(Vector3.zero, Vector3.forward * 100);
- if (Physics.Raycast (Vector3.zero, Vector3.forward, Mathf.Infinity)) {
- print ("There is something in front of the object!");
- }
- }
Struct
Representation of rays.
表示射线。
A ray is an infinite line starting at origin and going in some direction.
射线是一个无穷的线,开始于origin并沿着direction方向。
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));
- }
- }
function ToString () : string
function ToString (format : string) : string
Description描述
Returns a nicely formatted string for this ray.
返回该光线格式化好的字符串。
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层的不同来忽略碰撞体。
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。