本文整理匯總了C#中UnityEngine.Collider.Raycast方法的典型用法代碼示例。如果您正苦於以下問題:C# Collider.Raycast方法的具體用法?C# Collider.Raycast怎麽用?C# Collider.Raycast使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類UnityEngine.Collider
的用法示例。
在下文中一共展示了Collider.Raycast方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: existsBetween
public static bool existsBetween(Collider collider, Triangle3 triangle)
{
RaycastHit info;
return (
collider.Raycast(new Ray(triangle.A, triangle.B - triangle.A), out info, (triangle.B - triangle.A).magnitude) ||
collider.Raycast(new Ray(triangle.B, triangle.C - triangle.B), out info, (triangle.C - triangle.B).magnitude) ||
collider.Raycast(new Ray(triangle.C, triangle.A - triangle.C), out info, (triangle.A - triangle.C).magnitude)
);
}
示例2: collider_contains_pt
public static bool collider_contains_pt( Collider test, Vector3 point)
{
Vector3 center;
Vector3 direction;
Ray ray;
RaycastHit hitInfo;
bool hit;
center = test.bounds.center;
direction = center - point;
ray = new Ray(point, direction);
hit = test.Raycast(ray, out hitInfo, direction.magnitude);
return !hit;
}
示例3: PointIsInsideMeshCollider
/// <summary>
/// Returns whether the point is inside the mesh collider.
/// </summary>
private static bool PointIsInsideMeshCollider(Collider c, Vector3 p)
{
Vector3[] directions = { Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back };
foreach (var ray in directions)
{
RaycastHit hit;
if (c.Raycast(new Ray(p - ray * 1000, ray), out hit, 1000f) == false)
{
return false;
}
}
return true;
}
示例4: IsInside
public static bool IsInside(Collider test, Vector3 point)
{
Vector3 center;
Vector3 direction;
Ray ray;
RaycastHit hitInfo;
bool hit;
// Use collider bounds to get the center of the collider. May be inaccurate
// for some colliders (i.e. MeshCollider with a 'plane' mesh)
center = test.bounds.center;
// Cast a ray from point to center
direction = center - point;
ray = new Ray(point, direction);
hit = test.Raycast(ray, out hitInfo, direction.magnitude);
// If we hit the collider, point is outside. So we return !hit
return !hit;
}
示例5: ColliderLineCast
public static bool ColliderLineCast(Collider collider, Vector3 src, Vector3 des, out RaycastHit hit)
{
return collider.Raycast (new Ray (src, des-src), out hit, (des - src).magnitude);
}
示例6: CheckBodyPartHit
public void CheckBodyPartHit(Collider coll, Vector3 origin, Vector3 direction, float radius)
{
if (bulletCollision)
{
foreach (Transform child in shootables)
{
RaycastHit hit;
Ray ray = new Ray(origin, direction.normalized);
if (coll.Raycast(ray, out hit, radius))
{
//Debug.Log(child.gameObject);
}
}
}
}
示例7: IsPointAtColliderEdge
/// <summary>
/// Checks if point is at most <paramref name="tolerance"/> away from the collider edge.
/// </summary>
/// <param name="collider">
/// The collider to check against.
/// </param>
/// <param name="point">
/// Point being checked.
/// </param>
/// <param name="tolerance">
/// Maximal distance
/// </param>
/// <returns>
/// <c>true</c> if the <paramref name="point"/> is inside the <paramref name="collider"/>
/// and at most <paramref name="tolerance"/> away from its edge,
/// <c>false</c> otherwise.
/// </returns>
public static bool IsPointAtColliderEdge(Collider collider, Vector3 point, float tolerance) {
RaycastHit hit;
tolerance *= 0.71f; // Approximately 1/sqrt(2)
Vector3 direction = collider.bounds.center - point;
Vector3 directionNormalized = direction.normalized;
bool result = direction != Vector3.zero &&
collider.Raycast(
new Ray(point - directionNormalized * tolerance, directionNormalized),
out hit, tolerance
);
return result;
}
示例8: GetWorldPointFromMouse
private Vector3 GetWorldPointFromMouse(Collider obj_collider) {
float planeLevel = 0;
var groundPlane = new Plane(Vector3.up, new Vector3(0, planeLevel, 0));
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
Vector3 hit = new Vector3(0,0,0);
float dist;
RaycastHit rayHit;
if (obj_collider.Raycast(ray, out rayHit, Mathf.Infinity)) {
return rayHit.point;
} else if (groundPlane.Raycast(ray, out dist)) {
hit = ray.origin + ray.direction.normalized * dist;
}
return hit;
}
示例9: IsPointInsideCollider
/// <summary>
/// Checks whether the point is inside a collider.
/// </summary>
/// <remarks>
/// This method can check against all types of colliders,
/// including <c>TerrainCollider</c> and concave <c>MeshCollider</c>.
/// </remarks>
/// <param name="collider">
/// The collider to check against.
/// </param>
/// <param name="point">
/// The point being checked.
/// </param>
/// <param name="specificTest">
/// Defines a kind of specific collision test that must be done against <paramref name="collider"/>.
/// </param>
/// <returns>
/// <c>true</c> if the <paramref name="point"/> is inside the <paramref name="collider"/>,
/// <c>false</c> otherwise.
/// </returns>
public static bool IsPointInsideCollider(Collider collider, Vector3 point, ColliderSpecificTestEnum specificTest = ColliderSpecificTestEnum.None) {
RaycastHit hit;
if (specificTest == ColliderSpecificTestEnum.None) {
#if !UNITY_FLASH
if (collider is TerrainCollider) {
if (!collider.Raycast(new Ray(point, Vector3.up), out hit, collider.bounds.size.y)) {
return false;
}
} else
#endif
if (collider is MeshCollider && !((MeshCollider) collider).convex) {
if (!IsPointInsideMeshCollider(collider, point)) {
return false;
}
} else {
Vector3 direction = collider.bounds.center - point;
float directionMagnitude = direction.sqrMagnitude;
if (directionMagnitude > 0.0001f &&
collider.Raycast(new Ray(point, direction.normalized), out hit, FastFunctions.FastSqrt(directionMagnitude))) {
return false;
}
}
} else {
if (specificTest == ColliderSpecificTestEnum.Terrain) {
if (!collider.Raycast(new Ray(point + Vector3.up * collider.bounds.size.y, Vector3.down), out hit, collider.bounds.size.y)) {
return false;
}
}
}
return true;
}
示例10: IsPointInsideMeshCollider
/// <summary>
/// Checks whether the point is inside a MeshCollider.
/// </summary>
/// <param name="collider">
/// Collider to check against.
/// </param>
/// <param name="point">
/// Point being checked.
/// </param>
/// <returns>
/// <c>true</c> if the <paramref name="point"/> is inside the <paramref name="collider"/>,
/// <c>false</c> otherwise.
/// </returns>
public static bool IsPointInsideMeshCollider(Collider collider, Vector3 point) {
Ray rayCast = new Ray();
for (int i = 0; i < meshColliderCheckDirections.Length; i++) {
Vector3 dir = meshColliderCheckDirections[i];
rayCast.origin = point - dir * 1000f;
rayCast.direction = dir;
RaycastHit hit;
if (collider.Raycast(rayCast, out hit, 1000f) == false)
{
return false;
}
}
return true;
}
示例11: PtInCollider
static public bool PtInCollider( Collider col, Collider excludeCol, Ray ray, bool all=false)
{
bool ret = false;
if( true == all)
{
RaycastHit hit = new RaycastHit();
ret = col.Raycast( ray, out hit, 500.0f);
}
else
{
IComparer comp = new DepthComparer();
RaycastHit[] hits = Physics.RaycastAll( ray);
if( 0 == hits.Length)
return false;
Array.Sort( hits, comp);
int index = 0;
if( excludeCol == hits[0].collider)
index = 1;
ret = ( col == hits[ index].collider) ? true : false;
}
return ret;
}
示例12: UpdateToggleState
void UpdateToggleState(Collider collider, DiageticToggle toggle, Ray ray, Vector3 heading, Vector3 p, bool isActive, string handedness)
{
RaycastHit hit = new RaycastHit();
if (toggle.state != toggle.DRAGGING && isActive && collider.Raycast(ray, out hit, 200.0f))
{ // start a drag
toggle.state = toggle.DRAGGING;
toggle.OnGrab();
toggle.handUsed = handedness;
// do things related to starting a drag
}
if (toggle.state == toggle.DRAGGING && toggle.handUsed == handedness)
{
if (!isActive)
{ // no longer dragging
toggle.state = toggle.NORMAL;
performToggleAction(toggle);
}
}
}
示例13: BuildBridge
private static void BuildBridge(Collider bridgeCollider)
{
//Find min and max tiles
Tile maxTile = GetClosestTile (bridgeCollider.bounds.max);
Tile minTile = GetClosestTile (bridgeCollider.bounds.min);
bool leftToRight = bridgeCollider.bounds.size.x > bridgeCollider.bounds.size.z;
int firstMinNumber, firstMaxNumber, secondMinNumber, secondMaxNumber;
if (leftToRight)
{
firstMinNumber = minTile.I;
firstMaxNumber = maxTile.I;
secondMinNumber = minTile.J;
secondMaxNumber = maxTile.J;
}
else
{
firstMinNumber = minTile.J;
firstMaxNumber = maxTile.J;
secondMinNumber = minTile.I;
secondMaxNumber = maxTile.I;
}
for (int i = firstMinNumber; i<= firstMaxNumber; i++)
{
for (int j = secondMinNumber; j <= secondMaxNumber; j++)
{
Tile terrainTile;
if (leftToRight)
{
terrainTile = m_Grid[i,j];
}
else
{
terrainTile = m_Grid[j,i];
}
if (j != secondMinNumber && j != secondMaxNumber)
{
//Create tile and check if tile underneath is passable, we're raycasting as ClosestPointOnBounds wouldn't return the value we wanted if the bridge is sloped
Tile tileToAdd;
if (leftToRight)
{
Ray yValueRay = new Ray(m_Grid[i,j].Center+(Vector3.up*1000), Vector3.down);
RaycastHit hitInfo;
if (bridgeCollider.Raycast (yValueRay, out hitInfo, Mathf.Infinity))
{
tileToAdd = new Tile(i, j, new Vector3(m_Grid[i,j].Center.x, hitInfo.point.y, m_Grid[i,j].Center.z), true, false, bridgeCollider);
}
else
{
//We didn't hit the collider, that means it's either an entrance or exit, now we have to use ClosestPointOnBounds instead
tileToAdd = new Tile(i, j, new Vector3(m_Grid[i,j].Center.x, bridgeCollider.ClosestPointOnBounds (m_Grid[i,j].Center+(Vector3.up*10)).y, m_Grid[i,j].Center.z), true, false, bridgeCollider);
}
}
else
{
Ray yValueRay = new Ray(m_Grid[j,i].Center+(Vector3.up*1000), Vector3.down);
RaycastHit hitInfo;
if (bridgeCollider.Raycast (yValueRay, out hitInfo, Mathf.Infinity))
{
tileToAdd = new Tile(j, i, new Vector3(m_Grid[j,i].Center.x, hitInfo.point.y, m_Grid[j,i].Center.z), true, false, bridgeCollider);
}
else
{
tileToAdd = new Tile(j, i, new Vector3(m_Grid[j,i].Center.x, bridgeCollider.ClosestPointOnBounds (m_Grid[j,i].Center+(Vector3.up*10)).y, m_Grid[j,i].Center.z), true, false, bridgeCollider);
}
}
if (i == firstMinNumber || i == firstMaxNumber)
{
tileToAdd.BridgeTunnelEntrance = true;
}
terrainTile.LayeredTiles.Add(tileToAdd);
}
//Raycast to find height (but beware the bridge might not cover the center point)
//Since the bridge might not be over the center point, we'll raycast in 3 places
Vector3 startPoint = terrainTile.Center+(Vector3.down*5);
Ray rayCenter = new Ray(startPoint, Vector3.up);
Ray ray1;
Ray ray2;
RaycastHit hit;
if (leftToRight)
{
//ray1 wants to be above, ray2 below
Vector3 zHalfTileSize = new Vector3(0, 0, TileSize/2.0f);
ray1 = new Ray(startPoint + zHalfTileSize, Vector3.up);
ray2 = new Ray(startPoint - zHalfTileSize, Vector3.up);
}
else
{
//.........這裏部分代碼省略.........
示例14: StartGrabbing
protected void StartGrabbing(Kinect.Body body)
{
Debug.Log ("Start Grabbing");
//Kinect.Joint sourceJoint = body.Joints [Kinect.JointType.HandLeft];
Vector3 leftHandCoordinate = GetVector3FromJoint (body.Joints [Kinect.JointType.HandLeft]);
Vector3 rightHandCoordinate = GetVector3FromJoint (body.Joints [Kinect.JointType.HandRight]);
Vector3 curGrabPosition = (leftHandCoordinate + rightHandCoordinate) / 2.0f;
activeObject = GrabSearch(leftHandCoordinate, rightHandCoordinate);
//Debug.Log ("Grab Position: " + curGrabPosition);
if (activeObject == null) {
Debug.Log("No Active Object");
return;
}
Holdable_object holdable = activeObject.GetComponent<Holdable_object>();
grab_offset = Vector3.zero;
if(holdable == null){
Vector3 delta_position = activeObject.transform.position -curGrabPosition;
Ray grab_ray = new Ray (curGrabPosition, delta_position);
RaycastHit grab_hit;
if(activeObject.Raycast(grab_ray, out grab_hit, grabObjectDistance))
grab_offset = activeObject.transform.position - grab_hit.point;
else
grab_offset = activeObject.transform.position - curGrabPosition;
}
//wow... Quaternion
if (holdable != null) {
holdable.OnGrab();
}
}
示例15: GetHit
RaycastHit GetHit(Collider colliderToHit)
{
Vector3 position = transform.position;
Vector3 direction = colliderToHit.transform.position - position;
Ray ray = new Ray(position, direction);
RaycastHit hit;
colliderToHit.Raycast(ray, out hit, Mathf.Infinity);
if (Application.isEditor)
{
Debug.DrawRay(ray.origin, ray.direction, Color.magenta, 0.1F);
Debug.DrawRay(hit.point, hit.normal, Color.yellow, 0.1F);
}
return hit;
}