Search Unity

Artillery Adjustment Code

Discussion in 'Scripting' started by Seizure, Jan 17, 2013.

  1. Seizure

    Seizure

    Joined:
    Dec 18, 2012
    Posts:
    9
    As I found this to be very difficult I thought I would post the final answer and hopefully would help someone else.

    My set up is to have the player's position located at an empty game object named cube
    Based off the last (initial explosion location) you can move the adjustment left right and add/drop based on players view direction and not world space. Any questions please feel free to message me.

    C# Code:
    Code (csharp):
    1. void adjustFire (string dir, string dist, string alt)
    2.     {
    3.         numberDir = (float)System.Convert.ToInt32(dir);                                             //Converts our string left/right correction into a float
    4.         numberDist = (float)System.Convert.ToInt32(dist);                                           //Converts our string add/drop   correction into a float
    5.         tempVector = explosionVector;                                                               //Obtain last position of explosion
    6.         explosionClone = Instantiate(explosion,explosionVector,Quaternion.identity) as GameObject;  //Instantiate an explosion object
    7.         explosionClone.name = "explode";                                                            //Name is so we can delete later
    8.         explosionClone.transform.parent = selectedCube.transform;                                   //Parent to the OP so we can slew it left and right
    9.         vectorDistance = Vector3.Distance (explosionVector, myCamera.transform.position);           //Find the distance from last explosion to our OP
    10.         OTFactor = vectorDistance / 1000;                                                           //Find the Observer to Target Factor (This helps us convert to an angle)
    11.         if (OTFactor > 1)                                                                           //If the OTFactor is greater then 1 we will devide it
    12.         {
    13.             myAngle = numberDir / OTFactor;                                                         //Converts our string left/right correction into a float
    14.         }
    15.         else
    16.         {
    17.             myAngle = numberDir * OTFactor;                                                         //If its less then 1 then we have to multiply or it gives us a bad number
    18.         }  
    19.         if (leftRight)
    20.         {
    21.             myAngle = myAngle * -1;                                                                 //Determine whether we are going left or right
    22.         }
    23.         if (addDrop)
    24.         {
    25.             numberDist = numberDist * -1;                                                           //Determine whether we are going to add or drop
    26.         }
    27.         myAngle = myAngle / 17.77777777777778f;                                                     //Convert from mils to degrees                                         
    28.         tempVector.x = 0;                                                                          
    29.         tempVector.y = myAngle;                                                                     //Put our found angle into a vector so we can use it later
    30.         tempVector.z = 0;                                                                          
    31.         selectedCube.transform.eulerAngles += tempVector;                                           //Adjust our cubes rotation, use eulerangles based off of our given information
    32.         tempVector.x = 0;                                                                          
    33.         tempVector.y = 0;                                                                          
    34.         tempVector.z = numberDist;                                                                  //Put our distance adjustment into a vector so we can use it later
    35.         explosionClone.transform.localPosition += tempVector;                                       //Add our distance based off local position so as to add or subtract distance from our explosion
    36.         tempVector = explosionClone.transform.position;                                             //Find out what our new position is
    37.         tempVector.y = Terrain.activeTerrain.SampleHeight(explosionClone.transform.position);       //At our new position find the terrain height and store in the y value
    38.         explosionClone.transform.position = tempVector;                                             //reset our position including terrain elevation
    39.         explosionVector = explosionClone.transform.position;                                        //reset our last explosion place holder to the current explosion
    40.         explosionCounter = true;                                                                    //turn on count down until destroy explosion
    41.         timeCounter = Time.time;                                                                    //Set the timer
    42.     }