孙广东 2015.8.15
if (Physics.Raycast(transform.position, Vector3.forward, 10))
print("There is something in front of the object!");RaycastHit hitInfo; if (Physics.Raycast(transform.position, Vector3.down, out hitInfo)) print(“There is something ” + hitInfo.distance +”m away from gameobject”);
using UnityEngine;
using System.Collections;
public class CollisionDetector : MonoBehaviour
{
public float MovingForce;
Vector3 StartPoint;
Vector3 Origin;
public int NoOfRays = 10;
int i;
RaycastHit HitInfo;
float LengthOfRay, DistanceBetweenRays, DirectionFactor;
float margin = 0.015f;
Ray ray;
void Start ()
{
//Length of the Ray is distance from center to edge
LengthOfRay = collider.bounds.extents.y;
//Initialize DirectionFactor for upward direction
DirectionFactor = Mathf.Sign (Vector3.up.y);
}
void Update ()
{
// First ray origin point for this frame
StartPoint = new Vector3 (collider.bounds.min.x + margin,transform.position.y,transform.position.z);
if (!IsCollidingVertically ()) {
transform.Translate (Vector3.up * MovingForce * Time.deltaTime * DirectionFactor);
}
}
bool IsCollidingVertically ()
{
Origin = StartPoint;
DistanceBetweenRays = (collider.bounds.size.x - 2 * margin) / (NOofRays - 1);
for (i = 0; i<NOofRays; i++) {
// Ray to be casted.
ray = new Ray (Origin, Vector3.up * DirectionFactor);
//Draw ray on screen to see visually. Remember visual length is not actual length.
Debug.DrawRay (Origin, Vector3.up * DirectionFactor, Color.yellow);
if (Physics.Raycast (ray, out HitInfo, LengthOfRay)) {
print ("Collided With " + HitInfo.collider.gameObject.name);
// Negate the Directionfactor to reverse the moving direction of colliding cube(here cube2)
DirectionFactor = -DirectionFactor;
return true;
}
Origin += new Vector3 (DistanceBetweenRays, 0, 0);
}
return false;
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u010019717/article/details/47676797