给弹弓添加LineRenderer绘制拉线

 

 

//将下面的组件及脚本添加给子弹

    public float maxStretch = 3.0f;
    public LineRenderer catapultLineFront;
    public LineRenderer catapultLineBack;
    private SpringJoint2D spring;
    private Transform catapult;
    private Ray rayToMouse;
    private Ray leftCatapultToProjectile;
    private float maxStretchSqr;
    private float circleRadius;
    private bool clickedOn;
    private Vector2 prevVelocity;
    public Transform positPrefab;
    Transform[] points = new Transform[100];
    void Awake()
    {
        spring = GetComponent<SpringJoint2D>();
        catapult = spring.connectedBody.transform;
    }
    void Start()
    {
        LineRendererSetup();
        rayToMouse = new Ray(catapult.position, Vector3.zero);
        leftCatapultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero);
        maxStretchSqr = maxStretch * maxStretch;
        CircleCollider2D circle = GetComponent<Collider2D>() as CircleCollider2D;
        circleRadius = circle.radius;
        for (int i = 0; i < points.Length; i++) //初始化生成虚线点
        {
            points[i] = Instantiate(positPrefab);
        }
    }
    void Update()
    {
        if (clickedOn)
            Dragging();
        if (spring != null)
        {
            if (!GetComponent<Rigidbody2D>().isKinematic && prevVelocity.sqrMagnitude > GetComponent<Rigidbody2D>().velocity.sqrMagnitude)
            {
                Destroy(spring);
                GetComponent<Rigidbody2D>().velocity = prevVelocity;
            }
            //if (!clickedOn) //得到速度
            // prevVelocity = GetComponent<Rigidbody2D>().velocity;
            LineRendererUpdate();
        }
        else
        {
            catapultLineFront.enabled = false;
            catapultLineBack.enabled = false;
        }
    }
    void LineRendererSetup()
    {
        catapultLineFront.SetPosition(0, catapultLineFront.transform.position);
        catapultLineBack.SetPosition(0, catapultLineBack.transform.position);
        catapultLineFront.sortingLayerName = “Foreground”;
        catapultLineBack.sortingLayerName = “Foreground”;
        catapultLineFront.sortingOrder = 3;
        catapultLineBack.sortingOrder = 1;
    }
    void OnMouseDown()
    {
        spring.enabled = false;
        clickedOn = true;
    }
    void OnMouseUp()
    {
        spring.enabled = true;
        GetComponent<Rigidbody2D>().isKinematic = false;
        clickedOn = false;
    }
    void Dragging() //拖动
    {
        Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 catapultToMouse = mouseWorldPoint – catapult.position;
        if (catapultToMouse.sqrMagnitude > maxStretchSqr)
        {
            rayToMouse.direction = catapultToMouse;
            mouseWorldPoint = rayToMouse.GetPoint(maxStretch);
        }
        mouseWorldPoint.z = 0f;
        transform.position = mouseWorldPoint;
        Vector2 direction = catapult.position – transform.position; //得到发射的向量
        prevVelocity = 8 * direction;
        float t = 0.05F;//每0.1秒钟放置一个
        for (int i = 0; i < points.Length; i++)
        {
            Vector3 pos = new Vector3();
            pos.x = (i * 0.05F * prevVelocity.x);
            pos.y = (i * t * (prevVelocity.y + 0.5f * Physics2D.gravity.y * i * t));
            points[i].position = transform.position + pos;
        }
    }
    void LineRendererUpdate()
    {
        Vector2 catapultToProjectile = transform.position – catapultLineFront.transform.position;
        leftCatapultToProjectile.direction = catapultToProjectile;
        Vector3 holdPoint = leftCatapultToProjectile.GetPoint(catapultToProjectile.magnitude + circleRadius);
        catapultLineFront.SetPosition(1, holdPoint);
        catapultLineBack.SetPosition(1, holdPoint);
    }
}