从一点到其他旋转点的Raycast

时间:2015-02-05 15:41:04

标签: unity3d

我必须在游戏中创造激光束效果。

基本上我正在使用为此渲染的线条。我必须从玩家到某一点绘制线条,该点在屏幕上旋转。现在我必须在该点获得光线投射,以在线渲染中创建第三个反射顶点。

Physics2d.raycast不会返回确切的击球点。你能帮我解决一下如何在旋转物体上使用光线投射。我会非常感激。我试图计算角度,但仍有差异。

使用UnityEngine; 使用System.Collections;

public class RayDeflection : TouchHandler
{

        Vector3 p1, p2, orgPos;
        LineRenderer lineRenderer;

        protected const int HITS = 2;
        float RAY_LENGTH = 1.5f;
        // Use this for initialization
        void Start ()
        {
                PlayerScript = PlayerScript = GameObject.FindGameObjectWithTag ("PlayerScript").gameObject.GetComponent<PlayerControlScript> ();

                lineRenderer = transform.GetComponent<LineRenderer> ();
                Laser ();
        }
        protected override void OnTouchStart (Vector3 tapPosition, TouchHandler.ScreenTouchType touchType)
        {



        }

        protected override void OnTouchMoved (Vector3 tapPosition, Vector3 deltaMove, float duration, TouchHandler.ScreenTouchType touchType)
        {
                //RaycastCheck ();  
                Laser ();
                //Laser (tapPosition);
        }


        protected override void OnTouchEnd (Vector3 tapPosition, float duration, TouchHandler.ScreenTouchType touchType)
        {

        }



        // Update is called once per frame
        void Laser ()
        {

                //p1 = transform.position;
                orgPos = transform.position;        

                p1 = transform.right * RAY_LENGTH;


                {
                        RaycastHit2D hit = Physics2D.Raycast (orgPos, p1);

                        lineRenderer.SetPosition (0, transform.position);


                        //Debug.DrawRay (p1, p2);

                        //  
                        if (hit.collider != null) {         
                                //  Debug.LogError (hit.collider.name);

                                p1 = hit.point;
                                lineRenderer.SetPosition (1, p1);


                                Vector3 distVect = (transform.position - p1);
                                Vector2 shotVect = new Vector2 (distVect.x, distVect.y);    

                                Vector3 rVect = Vector3.Reflect ((p1 - orgPos), Vector3.right);
                                Debug.LogError (p1);

                                p2 = rVect * RAY_LENGTH;



                                //  Debug.DrawRay (p1, p2);

                                if (hit.collider.tag.Equals (Constant.tag.items)) {

                                        lineRenderer.SetVertexCount (3);
                                        //                      
                                        lineRenderer.SetPosition (2, p2);
                                } else {
                                        lineRenderer.SetVertexCount (2);
                                }
                        } else {
                                lineRenderer.SetVertexCount (2);
                        }


                }
        }
}

0 个答案:

没有答案