unity中碰撞学习总结,关于射线

2018-05-21  本文已影响0人  不过一书生

参考网站:https://blog.csdn.net/u014805066/article/details/52454537

我需要实现的是摄像机的中心发射射线作为鼠标来使用,并且在交点处实例化一个特征(一般用粒子效果)。

以下是我用的代码。

public GameObject sphere;//需要实例化的物体

void Update ()

    {

        Ray ray = new Ray(transform.position, transform.forward);

        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, Mathf.Infinity))

        {

            print(hit.point);//这是射线与物体交点的坐标

            Debug.Log(hit.collider.name);

            Debug.DrawLine(ray.origin, hit.point, Color.red);

            GameObject go = Instantiate(sphere);

            go.transform.position = new Vector3(hit.point.x, hit.point.y, hit.point.z);

            Destroy(go, 0.02f);

        }

    }

考虑到,物体远近不一,特征效果需要变换大小,使之便于识别,采用距离来作为参数进行控制特征效果大小,distance 从射线起点到射线与碰撞器的交点的距离。

上一篇下一篇

猜你喜欢

热点阅读