Search Unity

Question Lerp Bullet between 5000+ position with specified time interval in millisecond.

Discussion in 'Scripting' started by unity_NQxq0pGcBaIDXQ, Sep 7, 2021.

  1. unity_NQxq0pGcBaIDXQ

    unity_NQxq0pGcBaIDXQ

    Joined:
    Nov 21, 2020
    Posts:
    1
    Code (CSharp):
    1. [BurstCompile]
    2.     public struct MoveToTargetJob : IJob
    3.     {
    4.         public Vector3 pos;
    5.         public NativeArray<Vector3> pathPoints;
    6.  
    7.         public float time;
    8.         public NativeArray<Vector3> resultArray;
    9.         public NativeArray<Vector3> cubePosition;
    10.         public NativeArray<int> index;
    11.  
    12.         public float currentTime;
    13.         void IJob.Execute()
    14.         {
    15.             currentTime += time;
    16.  
    17.             float percentComplete = currentTime/0.001f;
    18.  
    19.             percentComplete = Mathf.Clamp01(percentComplete); // this prevents it exceeding 1
    20.  
    21.             if (pathPoints.Length > index[0])
    22.             {
    23.                 resultArray[0] = Vector3.Lerp(cubePosition[0], pathPoints[index[0]], percentComplete);
    24.  
    25.                 var distance = Vector3.Distance(cubePosition[0], pathPoints[index[0]]);
    26.  
    27.                 if (distance <= 0.1f)
    28.                 {
    29.                     index[0] = index[0] + 30;
    30.                     currentTime = 0;
    31.                 }
    32.             }
    33.  
    34.         }
    35.     }

    Code (CSharp):
    1.  public void FixedUpdate()
    2.     {    
    3.         if (m_BallisticServerData != null && m_BallisticServerData.data1.Count > 0)
    4.         {
    5.             if (mUseJobs)
    6.             {
    7.                 NativeArray<Vector3> result = new NativeArray<Vector3>(1, Allocator.TempJob);
    8.                 NativeArray<Vector3> pathPts = new NativeArray<Vector3>(m_BallisticServerData.data1.Count, Allocator.TempJob);
    9.                 NativeArray<int> pointIndex = new NativeArray<int>(1, Allocator.TempJob);
    10.                 NativeArray<Vector3> cubePos = new NativeArray<Vector3>(1, Allocator.TempJob);
    11.  
    12.                 for (int i = 0; i < m_BallisticServerData.data1.Count; i++)
    13.                 {
    14.                     Vector3 pos = new Vector3(m_BallisticServerData.data1[i].pos.x, m_BallisticServerData.data1[i].pos.y, m_BallisticServerData.data1[i].pos.z);
    15.                     pathPts[i] = pos;
    16.                 }
    17.  
    18.                 pointIndex[0] = indexOuter;
    19.  
    20.                 cubePos[0] = Cube.transform.position;
    21.  
    22.                 job = new MoveToTargetJob()
    23.                 {
    24.  
    25.                     pathPoints = pathPts,
    26.                     time = mTime,
    27.                     index = pointIndex,
    28.                     cubePosition = cubePos,
    29.                     resultArray = result
    30.                 };
    31.  
    32.                 jobHandle = job.Schedule();
    33.                 jobHandle.Complete();
    34.  
    35.                 Cube.transform.position = result[0];
    36.  
    37.                 indexOuter = pointIndex[0];
    38.  
    39.  
    40.                 cubePos.Dispose();
    41.                 result.Dispose();
    42.                 pathPts.Dispose();
    43.                 pointIndex.Dispose();
    44.             }
    45.         }
    So i want to move an object similar like a Bullet with its muzzle velocity specified between two position. where i will be receiving a continuous data in the from json, in the form of 5000+ trajectory position and its time to travel between each two points.
    I tried using multiple ways like using coroutines, in Update , Fixed Update and using Job system and burst compiler , its showing the same result and result is time taken to travel a bullet from start to end is more . Lerp or MoveTowards showing slower movement with smaller time given that is in milliseconds.