Search Unity

Spline!Object to follow

Discussion in 'Scripting' started by winman, Mar 24, 2012.

  1. winman

    winman

    Joined:
    Mar 20, 2012
    Posts:
    11
    This is a spline

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CRSpline : MonoBehaviour
    5. {
    6.     ArrayList vp;
    7.     float delta_t;
    8.     bool dragging;
    9.     int dragCount;
    10.     Vector3 previousDrag;
    11.     Vector3[] vertices;
    12.     int num_verts;
    13.     LineRenderer line;
    14.  
    15.     void Start()
    16.     {
    17.         vp = new ArrayList();
    18.         dragging = false;
    19.         vertices = new Vector3[202];
    20.         num_verts = 0;
    21.     }
    22.  
    23.     void Update()
    24.     {    
    25.         Vector3 pos = Input.mousePosition;
    26.         Vector3 p = Camera.main.ScreenToWorldPoint(new Vector3(pos.x, pos.y, pos.z));
    27.  
    28.         if (Input.GetMouseButtonDown(0))
    29.         {
    30.             OnTouchBegin(p);
    31.         }
    32.         else if (Input.GetMouseButton(0))
    33.         {
    34.             OnTouchMove(p);
    35.         }
    36.         else if (Input.GetMouseButtonUp(0))
    37.         {
    38.             OnTouchEnd(p);
    39.         }
    40.     }
    41.  
    42.     void OnTouchBegin(Vector3 p)
    43.     {
    44.         dragCount = 0;
    45.         Clear();
    46.         AddSplinePoint(p);
    47.         dragging = true;
    48.     }
    49.    
    50.     void OnTouchMove(Vector3 p)
    51.     {
    52.         if (dragging)
    53.         {
    54.             AddSplinePoint(p);
    55.             if (dragCount < 50)
    56.             {
    57.                 if (Vector3.Distance(p, previousDrag) >= 7)
    58.                 {
    59.                     print("aa");
    60.                     AddSplinePoint(p);
    61.                     dragCount++;
    62.                     previousDrag = p;
    63.                 }
    64.             }
    65.         }
    66.     }
    67.    
    68.     void OnTouchEnd(Vector3 p)
    69.     {
    70.         StopDraw();
    71.         dragging = false;
    72.     }
    73.    
    74.  
    75.     // Solve the Catmull-Rom parametric equation for a given time(t) and vector quadruple (p1,p2,p3,p4)
    76.     Vector3 Eq(float t, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4)
    77.     {
    78.         float t2 = t * t;
    79.         float t3 = t2 * t;
    80.  
    81.         float b1 = 0.5f * (-t3 +  2 * t2 - t);
    82.         float b2 = 0.5f * (  3 * t3 - 5 * t2 + 2);
    83.         float b3 = 0.5f * ( -3 * t3 + 4 * t2 + t);
    84.         float b4 = 0.5f * (t3 - t2);
    85.  
    86.         return (p1 * b1 + p2 * b2 + p3 * b3 + p4 * b4);
    87.     }
    88.  
    89.     void AddSplinePoint(Vector3 v)
    90.     {
    91.         vp.Add(v);
    92.         delta_t = 1.0f / vp.Count;
    93.     }
    94.  
    95.     int BOUNDS(int p)
    96.     {
    97.         int pp = p;
    98.         if (p < 0) pp = 0;
    99.         else if (p >= vp.Count - 1) pp = vp.Count - 1;
    100.         return pp;
    101.     }
    102.    
    103.     Vector3 GetInterpolatedSplinePoint(float t)
    104.     {
    105.         // Find out in which interval we are on the spline
    106.         int p = (int)(t / delta_t);
    107.         // Compute local control point indices
    108.  
    109.         int p0 = p - 1;
    110.         p0 = BOUNDS(p0);
    111.         int p1 = p;
    112.         p1 = BOUNDS(p1);
    113.         int p2 = p + 1;
    114.         p2 = BOUNDS(p2);
    115.         int p3 = p + 2;
    116.         p3 = BOUNDS(p3);
    117.         // Relative (local) time
    118.         float lt = (t - delta_t * (float)p) / delta_t;
    119.         // Interpolate
    120.         return Eq(lt, (Vector3)vp[p0], (Vector3)vp[p1], (Vector3)vp[p2], (Vector3)vp[p3]);
    121.     }
    122.  
    123.     public int GetNumPoints()
    124.     {
    125.         return vp.Count;
    126.     }
    127.  
    128.     public Vector3 GetNthPoint(int n)
    129.     {
    130.         return (Vector3)vp[n];
    131.     }
    132.  
    133.     public void SetNthPoint(int n, Vector3 v)
    134.     {
    135.         vp[n] = v;
    136.     }
    137.    
    138.     void RemoveFirst()
    139.     {
    140.         vp.RemoveAt(0);
    141.     }
    142.  
    143.     public void Clear()
    144.     {
    145.         vp.Clear();
    146.         num_verts = 0;
    147.     }
    148.  
    149.     void StopDraw()
    150.     {
    151.         // Calc the full curve
    152.         num_verts = 0;
    153.  
    154.         if (GetNumPoints() > 3)
    155.         {
    156.             for (float t = 0; t < 1.0f; t += 1.0f/25.0f)
    157.             {
    158.                 Vector3 a = GetInterpolatedSplinePoint(t);
    159.                 vertices[num_verts].x = a.x;
    160.                 vertices[num_verts].y = a.y;
    161.                 vertices[num_verts].z = 0;
    162.                 num_verts++;
    163.                
    164.             }
    165.            
    166.             line = GetComponent(typeof(LineRenderer)) as LineRenderer;
    167.             line.SetWidth(10f, 10f);
    168.        
    169.             line.SetVertexCount(num_verts);
    170.             for (int i = 0; i < num_verts; i++)
    171.             {
    172.                 line.SetPosition(i, vertices[i]);
    173.             }
    174.         }
    175.     }
    176. }
    Now I want Object follow the path .
    How to write ?
    Can give me some suggest or write Script to me?
    thanks!
     
    Last edited: Mar 26, 2012
  2. Zerot

    Zerot

    Joined:
    Jul 13, 2011
    Posts:
    135
    Well, you already have a GetInterpolatedSplinePoint method, so the only thing you have to do, is to write a script that sets the position to the results of that method.
     
  3. winman

    winman

    Joined:
    Mar 20, 2012
    Posts:
    11
    Can write some code for me?