射线检测

using UnityEngine;
using System.Collections;

public class RayCastDemo : MonoBehaviour {

    //射线碰撞检测器
    RaycastHit hit;

    void Update()
    {
        //通过鼠标屏幕坐标,生成一条射线对象
        Ray r = Camera.main.ScreenPointToRay (Input.mousePosition);
        //发射射线,并检测
        if (Physics.Raycast (r, out hit)) {
            //获取射线起点
            Vector3 begin = Camera.main.transform.position;
            //获取射线终点
            Vector3 end = hit.point;
            //根据起点终点划线
            Debug.DrawLine (begin, end,Color.red);
        }
    }
}

 

posted @ 2016-10-29 16:31  扎北强子  阅读(192)  评论(0编辑  收藏  举报