コード例 #1
0
ファイル: Demo.cs プロジェクト: wty0512/unity-bezier-curves
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         position.z = transform.position.z;
         _curveCount++;
         _curvePoints[_curveCount] = position;
         if (_curveCount == 3)
         {
             _curveCount = -1;
             lines.AddCurve(_curvePoints, Color.red, Color.blue);
         }
     }
 }
コード例 #2
0
    /// <summary>
    /// Draws the bezier ray.
    /// </summary>
    private void DrawBezierRay()
    {
        bezierRay.RemoveCurves();

        if (targetInteractable == null)
        {
            return;
        }

        Vector3 pointOnLine = NearestPointOnLine(primaryHand.transform.position, primaryHand.transform.forward,
                                                 targetInteractable.transform.position);
        Vector3 dir   = targetInteractable.transform.position - pointOnLine;
        float   dist1 = dir.magnitude;
        float   dist2 = Vector3.Distance(primaryHand.transform.position, pointOnLine);

        Vector3[] bezierPoints = new Vector3[4];
        bezierPoints[0] = primaryHand.transform.position;
        bezierPoints[1] = primaryHand.transform.position + dist2 * (1f / 3f) * primaryHand.transform.forward.normalized;
        bezierPoints[2] = primaryHand.transform.position + dist1 * (2f / 3f) * dir.normalized +
                          dist2 * (2f / 3f) * primaryHand.transform.forward.normalized;
        bezierPoints[3] = targetInteractable.transform.position;

        bezierRay.AddCurve(bezierPoints, Color.red, Color.red, 0.005f, 0.0025f);
    }