Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Coding a projectile to travel through multiple vector

Discussion in 'Scripting' started by cinco, Aug 27, 2015.

  1. cinco

    cinco

    Joined:
    Apr 11, 2013
    Posts:
    11
    I am in the process of creating a realistic single player shooting app.

    The app already generates an array in order to determine the elevation and distance the projectile will travel. For instance:

    Start position - will be at mytransform.
    1st location - 50 units forward of start position, 100 units up of start position.
    2nd location - 100 units forward of start position , 150 units up of start position.
    3rd location - 150 units forward of start position , 200 units up of start position.

    //then coming down

    4th location - 200 units forward of start position , 150 units up of start position.
    5th location - 250 units forward of start position , 100 units up of start position.
    6th location - 300 units forward of start position, 50 units up of start position.
    End position - Impact

    And so on and so forth.

    If someone could point me in the right direction with some vector math code in order to get me started I would be grateful.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why are you storing positions in an array in order to work with projectile trajectories? seems like an odd approach.
     
  3. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    Are these start positions?
    Or are you trying to describe projectile movement through these points in array?
     
  4. cinco

    cinco

    Joined:
    Apr 11, 2013
    Posts:
    11
    The positions are created by a complex system in order to generate a perfect ballistic profile. It may seem like an odd approach, however, I can assure you for what I am trying to do, it is really the only approach.

    When the player clicks 'fire' a two dimensional array is generated that holds everything from the time a "real" bullet would take to travel from position to position, the elevation of the bullet in relation to the bore height, the remaining energy the bullet has at each point, distance traveled, etc...

    Bear in mind that this ballistic profile is gnat's ass accurate. All that is left is the vector math that will allow the bullet to travel from point to point.
     
  5. cinco

    cinco

    Joined:
    Apr 11, 2013
    Posts:
    11
    There is only one start position. I am trying to describe the parabolic arc of a bullet. All that is left is the vector math which should just be a loop the array from point to point, which is what I am trying to accomplish.
     
  6. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
  7. cinco

    cinco

    Joined:
    Apr 11, 2013
    Posts:
    11
    Thank you for the reply. I will look into Vector3.MoveTowards. However, what I really need is for the projectile to travel from point to point depening on the units in my original post.
     
  8. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Are the vectors you save in the array world coordinates, or the distances to the next point? If the first, then you can use those values with the idea I presented, and if the second you can just calculate the world coordinates by taking your transforms world coordinate and increment it with the values from your array to get the word coordinates so you can use them for the idea..
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Does the bullet have rigid body & physics interactions or are you trying to manually create the natural arc of the bullet ignoring its mass, gravity etc by plotting the curve as if the player is always holding the gun level when firing & the bullet always travels the same distance? Will you need to introduce things like friction/drag, wind effects, different profiled bullets & calibre?
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sounds like you want a spline or a beizer curve.

    You can cheat and get some of this functionality for free by adding your points to an animation curve and reading the points off at various time intervals.

    Or if you points are close enough you could just move the transform directly with Transform.position.
     
  11. cinco

    cinco

    Joined:
    Apr 11, 2013
    Posts:
    11
    They are distances. My array holds the distance forward, the height or distance up and the distance left/right depending on wind and other effects on projectiles.

    So right now for initial testing purposes I have this:

    //note that all of my data are stored as type double..
    Code (csharp):
    1.  
    2.     void CreateVectorArray (int x)
    3.     {
    4.        //So the intent here is to create an vector3 array which holds all the location data for the bullet on its way to the target.
    5.  
    6.         Vector3[] positions = new Vector3[x];
    7.  
    8.         positions[0] = this.gameObject.transform.position;
    9.  
    10.         for(int i = 0; i < 5; i++)
    11.         {
    12.             positions[i+1].x = positions[i].x * (float)ptr[0,i];//this array holds the distance forward
    13.             positions[i+1].y = positions[i].y *(float)ptr[9,i];//this array holds the distance up
    14.             positions[i+1].z = positions[i].z *(float)ptr[4,i];//this location holds the distance left and right
    15.         }
    16.         for(int j =0; j < 4; j++)
    17.         {
    18.             Debug.DrawLine(positions[j], positions[j+1], Color.red);
    19.             Debug.Log ("position[]" + positions[0]);
    20.  
    21.         }
    22.      
    23.     }
    24. [\code]