# Bounds.IntersectRay 相交射线？

function IntersectRay (ray : Ray) : bool

Description描述

Does ray intersect this bounding box?

``````using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public Ray ra = new Ray(Vector3.zero, Vector3.forward);
void Update() {
Debug.DrawRay(Vector3.zero, Vector3.forward * 999, Color.green);
Bounds bounds = transform.collider.bounds;
if (bounds.IntersectRay(ra))
Debug.Log("Touched the ray");

}
}``````
``````// Creates a ray that points from the origin to the infinity among the z Axis.
// And prints if the transform touched the ray.
//沿着z轴，从原点到无限远，创建一条射线
//并且如果变换碰到射线，打印消息
var ra : Ray = new Ray (Vector3.zero, Vector3.forward);

function Update () {
// Color ra in the scene editor.
//在场景编辑器，给射线一个颜色
Debug.DrawRay (Vector3.zero, Vector3.forward * 999, Color.green);
var bounds : Bounds = transform.collider.bounds;
if (bounds.IntersectRay (ra))
Debug.Log("Touched the ray");
}``````

• function IntersectRay (ray : Ray, out distance : float) : bool

Description描述

Does ray intersect this bounding box?

When IntersectRay returns true distance will be the distance to the ray's origin.

``````using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public Ray ra = new Ray(Vector3.zero, Vector3.forward);
public float t = 10.0F;
void Update() {
Debug.DrawRay(Vector3.zero, Vector3.forward * 10, Color.green);
Bounds bounds = transform.collider.bounds;
if (bounds.IntersectRay(ra, ref t))
Debug.Log("Touched the ray");

}
}``````
``````// Creates a ray that points from the origin to 10 units among the z Axis.
// And prints if the transform touched the ray.
//沿着z轴，从原点到10个单位之间，创建一条射线
//并且如果变换碰到射线，打印消息
var ra : Ray = new Ray (Vector3.zero, Vector3.forward);
var t : float = 10.0;

function Update () {
// Color ra in the scene editor.
//在场景编辑器，给射线一个颜色
Debug.DrawRay (Vector3.zero, Vector3.forward * 10, Color.green);
var bounds : Bounds = transform.collider.bounds;
if (bounds.IntersectRay (ra, t))
Debug.Log("Touched the ray");
}``````
Page last updated: 2010-12-29