当前位置:   article > 正文

Raycast光线投射,Linecast,RaycastAll_光射投影raycast

光射投影raycast

RaycastHit 光线投射碰撞

Struct

Structure used to get information back from a raycast.

用来获取从raycast函数中得到的信息反馈的结构。

参见:Physics.RaycastPhysics.LinecastPhysics.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 : Vector3direction : Vector3distance : float = Mathf.InfinitylayerMask : int = kDefaultRaycastLayers) : bool 

Parameters参数

  • origin
    The starting point of the ray in world coordinates.
    在世界坐标,射线的起始点。
  • direction
    The direction of the ray.
    射线的方向。
  • distance
    The length of the ray
    射线的长度。
  • layerMask
    A 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.

在场景中投下可与所有碰撞器碰撞的一条光线。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Example : MonoBehaviour {
  4. void Update() {
  5. Vector3 fwd = transform.TransformDirection(Vector3.forward);
  6. if (Physics.Raycast(transform.position, fwd, 10))
  7. print("There is something in front of the object!");
  8. }
  9. }

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 : Vector3direction : Vector3out hitInfo : RaycastHitdistance : float = Mathf.InfinitylayerMask : int = kDefaultRaycastLayers) : bool 

Parameters参数

  • origin
    The starting point of the ray in world coordinates.
    在世界坐标,射线的起始点。
  • direction
    The direction of the ray.
    射线的方向。
  • distance
    The length of the ray
    射线的长度。
  • hitInfo
    If true is returned, hitInfo will contain more information about where the collider was hit (See Also:  RaycastHit).
    如果返回true,hitInfo将包含碰到器碰撞的更多信息。 
  • layerMask
    A 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.

在场景中投下可与所有碰撞器碰撞的一条光线,并返回碰撞的细节信息。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Example : MonoBehaviour {
  4. void Update() {
  5. RaycastHit hit;
  6. if (Physics.Raycast(transform.position, -Vector3.up, out hit))
  7. float distanceToGround = hit.distance;
  8. }
  9. }

又一个例子:

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Example : MonoBehaviour {
  4. void Update() {
  5. RaycastHit hit;
  6. if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0F))
  7. float distanceToGround = hit.distance;
  8. }
  9. }

• static function Raycast (ray : Raydistance : float = Mathf.InfinitylayerMask : int = kDefaultRaycastLayers) : bool 

Parameters参数

  • ray
    The starting point and direction of the ray.
    射线的起点和方向
  • distance
    The length of the ray
    射线的长度。
  • layerMask
    A 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。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Example : MonoBehaviour {
  4. void Update() {
  5. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  6. if (Physics.Raycast(ray, 100))
  7. print("Hit something");
  8. }
  9. }

• static function Raycast (ray : Rayout hitInfo : RaycastHitdistance : float = Mathf.InfinitylayerMask : int = kDefaultRaycastLayers) : bool 

Parameters参数

  • ray
    The starting point and direction of the ray.
    射线的起点和方向 
  • distance
    The length of the ray
    射线的长度
  • hitInfo
    If true is returned, hitInfo will contain more information about where the collider was hit (See Also:  RaycastHit).
    如果返回true,hitInfo将包含碰到器碰撞的更多信息。 
  • layerMask
    A 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。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Example : MonoBehaviour {
  4. void Update() {
  5. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  6. RaycastHit hit;
  7. if (Physics.Raycast(ray, out hit, 100))
  8. Debug.DrawLine(ray.origin, hit.point);
  9. }
  10. }

Mathf.Infinity 正无穷

static var Infinity : float 

Description描述

A representation of negative infinity (Read Only).

表示正无穷,也就是无穷大,∞ (只读)[in'finiti]

  1. // Casts a ray from (0,0,0) towards (0,0,1) to the infinity and prints a message
  2. //画一条射线从(0,0,0) 向 (0,0,1)到无穷远,并打印一个消息
  3. // if any object has touched the ray.
  4. //如果任意物体碰到这个射线
  5. // To test it, just place any object and intersect it with the white drawn line
  6. //为便于测试,只需放置任意物体相交于白色的绘制线
  7. function Update () {
  8. // shows the line that follows the ray.
  9. //显示这条射线
  10. Debug.DrawLine(Vector3.zero, Vector3.forward * 100);
  11. if (Physics.Raycast (Vector3.zero, Vector3.forward, Mathf.Infinity)) {
  12. print ("There is something in front of the object!");
  13. }
  14. }

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构造器

  • Ray
    Creates a ray starting at origin along direction.
    创建一个新的射线,开始于 origin沿着 direction的方向。 

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距离单位的点。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class example : MonoBehaviour {
  4. public Ray r;
  5. public void Awake() {
  6. print(r.GetPoint(10));
  7. }
  8. }

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 : Vector3end : Vector3layerMask : int = kDefaultRaycastLayers) : bool 

Description描述

Returns true if there is any collider intersecting the line between start and end.

从开始位置到结束位置做一个光线投射,如果与碰撞体交互,返回真。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class example : MonoBehaviour {
  4. public Transform target;
  5. void Update() {
  6. if (!Physics.Linecast(transform.position, target.position))
  7. ProcessData.AndDoSomeCalculations();
  8. }
  9. }

Layer mask is used to selectively ignore colliders when casting a ray.

可以根据Layer mask层的不同来忽略碰撞体。

• static function Linecast (start : Vector3end : Vector3out hitInfo : RaycastHitlayerMask : 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 : Raydistance : 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[]结构体。

  1. function Update () {
  2. var hits : RaycastHit[];
  3. hits = Physics.RaycastAll (transform.position, transform.forward, 100.0);
  4. // Change the material of all hit colliders
  5. // to use a transparent Shader
  6. //改变所有碰到碰撞器的材质,使用透明的着色器
  7. for (var i = 0;i < hits.Length; i++) {
  8. var hit : RaycastHit = hits[i];
  9. var renderer = hit.collider.renderer;
  10. if (renderer) {
  11. renderer.material.shader = Shader.Find("Transparent/Diffuse");
  12. renderer.material.color.a = 0.3;
  13. }
  14. }
  15. }

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




声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号