一.射线回顾:
3D世界中一个点向一个方向发射无终点的线。在unity3d中我们发射的射线一旦与其他的碰撞器发生碰撞,射线将停止发射。在游戏制作过程中我们可以通过判断射线是否发生了碰撞,并且可以判断射线和谁发生了碰撞。应用范围非常广泛,如射击类游戏中用它来判断是否射中目标。
游戏中发射一条射线的两个因素:
(1)射线起点Ray.origion
(2)发射方向Ray.direction
void Update ()
{
//第一步:定义一条射线,起点为Vector3.zero,发射方向为朝向该脚本添加的游戏对象位置
Ray ray=new Ray(Vector3.zero,transform.position);
//第二步:定义一个光线投射碰撞
RaycastHit hit;
//第三步:发射射线
Physics.Raycast(ray,out hit,100);//返回的是一个bool值
//在Scene中生成这条射线,起点为射线的起点,终点为射线与物体的碰撞点
Debug.DrawLine(ray.origin,hit.point); //只在Scene视图中才可以看到
}
二、对Raycast 光线投射的总结(这里总结两种使用方式)
static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : 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.
在场景中投下可与所有碰撞器碰撞的一条光线。
C#
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!");
}
}function Update () {
var 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参数
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.
在场景中投下可与所有碰撞器碰撞的一条光线,并返回碰撞的细节信息。
C#
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;
}
}function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
var distanceToGround = hit.distance;
}
}
又一个例子:
C#
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;
}
}// Raycast up to 100 meters forward
//光线向前投射100米远
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
var distanceToGround = hit.distance;
}
}