Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Launch script with OnSelectionObject

Discussion in 'Scripting' started by simone_fratoni, May 4, 2020.

  1. simone_fratoni

    simone_fratoni

    Joined:
    Jan 17, 2020
    Posts:
    13
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AnimazioneProva : MonoBehaviour
    6. {
    7.     int value = 71;
    8.     private Vector3[] Spostamento = new Vector3[71];
    9.  
    10.     void Start()
    11.     {
    12.         Spostamento[1] = new Vector3(0f, 0, 0f);
    13.         Spostamento[2] = new Vector3(0.000004546f, 0, 0.000003637f);
    14.         Spostamento[3] = new Vector3(0.0000219f, 0, 0.00001627f);
    15.         Spostamento[4] = new Vector3(0.00005408f, 0, 0.00003682f);
    16.         Spostamento[5] = new Vector3(0.00009762f, 0, 0.00006111f);
    17.         Spostamento[6] = new Vector3(0.0001492f, 0, 0.00008703f);
    18.         Spostamento[7] = new Vector3(0.000206f, 0, 0.0001135f);
    19.         Spostamento[8] = new Vector3(0.0002659f, 0, 0.00014f);
    20.         Spostamento[9] = new Vector3(0.0003273f, 0, 0.0001661f);
    21.         Spostamento[10] = new Vector3(0.0003893f, 0, 0.0001916f);
    22.         Spostamento[11] = new Vector3(0.0004515f, 0, 0.0002172f);
    23.         Spostamento[12] = new Vector3(0.0005137f, 0, 0.0002433f);
    24.         Spostamento[13] = new Vector3(0.0005758f, 0, 0.0002709f);
    25.         Spostamento[14] = new Vector3(0.0006378f, 0, 0.0003006f);
    26.         Spostamento[15] = new Vector3(0.0006992f, 0, 0.000333f);
    27.         Spostamento[16] = new Vector3(0.0007598f, 0, 0.0003684f);
    28.         Spostamento[17] = new Vector3(0.0008197f, 0, 0.0004071f);
    29.         Spostamento[18] = new Vector3(0.0008781f, 0, 0.0004484f);
    30.         Spostamento[19] = new Vector3(0.0009339f, 0, 0.0004912f);
    31.         Spostamento[20] = new Vector3(0.0009861f, 0, 0.0005347f);
    32.         Spostamento[21] = new Vector3(0.00103f, 0, 0.000578f);
    33.         Spostamento[22] = new Vector3(0.00108f, 0, 0.0006209f);
    34.         Spostamento[23] = new Vector3(0.00112f, 0, 0.000663f);
    35.         Spostamento[24] = new Vector3(0.00116f, 0, 0.0007042f);
    36.         Spostamento[25] = new Vector3(0.00119f, 0, 0.0007449f);
    37.         Spostamento[26] = new Vector3(0.00123f, 0, 0.0007859f);
    38.         Spostamento[27] = new Vector3(0.00127f, 0, 0.0008278f);
    39.         Spostamento[28] = new Vector3(0.00131f, 0, 0.0008708f);
    40.         Spostamento[29] = new Vector3(0.00135f, 0, 0.0009145f);
    41.         Spostamento[30] = new Vector3(0.0014f, 0, 0.0009584f);
    42.         Spostamento[31] = new Vector3(0.00144f, 0, 0.001f);
    43.         Spostamento[32] = new Vector3(0.00149f, 0, 0.00104f);
    44.         Spostamento[33] = new Vector3(0.00154f, 0, 0.00108f);
    45.         Spostamento[34] = new Vector3(0.00158f, 0, 0.00111f);
    46.         Spostamento[35] = new Vector3(0.00161f, 0, 0.00113f);
    47.         Spostamento[36] = new Vector3(0.00163f, 0, 0.00115f);
    48.         Spostamento[37] = new Vector3(0.00163f, 0, 0.00116f);
    49.         Spostamento[38] = new Vector3(0.00164f, 0, 0.00116f);
    50.         Spostamento[39] = new Vector3(0.00163f, 0, 0.00117f);
    51.         Spostamento[40] = new Vector3(0.00163f, 0, 0.00117f);
    52.         Spostamento[41] = new Vector3(0.00163f, 0, 0.00118f);
    53.         Spostamento[42] = new Vector3(0.00165f, 0, 0.0012f);
    54.         Spostamento[43] = new Vector3(0.00167f, 0, 0.00122f);
    55.         Spostamento[44] = new Vector3(0.00171f, 0, 0.00124f);
    56.         Spostamento[45] = new Vector3(0.00177f, 0, 0.00127f);
    57.         Spostamento[46] = new Vector3(0.00184f, 0, 0.0013f);
    58.         Spostamento[47] = new Vector3(0.00194f, 0, 0.00133f);
    59.         Spostamento[48] = new Vector3(0.00204f, 0, 0.00136f);
    60.         Spostamento[49] = new Vector3(0.00214f, 0, 0.00138f);
    61.         Spostamento[50] = new Vector3(0.00223f, 0, 0.00138f);
    62.         Spostamento[51] = new Vector3(0.00228f, 0, 0.00135f);
    63.         Spostamento[52] = new Vector3(0.00228f, 0, 0.0013f);
    64.         Spostamento[53] = new Vector3(0.00224f, 0, 0.00122f);
    65.         Spostamento[54] = new Vector3(0.00216f, 0, 0.00113f);
    66.         Spostamento[55] = new Vector3(0.00204f, 0, 0.00103f);
    67.         Spostamento[56] = new Vector3(0.00191f, 0, 0.0009368f);
    68.         Spostamento[57] = new Vector3(0.00178f, 0, 0.0008613f);
    69.         Spostamento[58] = new Vector3(0.00167f, 0, 0.000812f);
    70.         Spostamento[59] = new Vector3(0.00159f, 0, 0.0007895f);
    71.         Spostamento[60] = new Vector3(0.00156f, 0, 0.0007858f);
    72.         Spostamento[61] = new Vector3(0.00155f, 0, 0.0007905f);
    73.         Spostamento[62] = new Vector3(0.00155f, 0, 0.0007932f);
    74.         Spostamento[63] = new Vector3(0.00157f, 0, 0.0007858f);
    75.         Spostamento[64] = new Vector3(0.00158f, 0, 0.0007628f);
    76.         Spostamento[65] = new Vector3(0.00159f, 0, 0.0007193f);
    77.         Spostamento[66] = new Vector3(0.00158f, 0, 0.0006496f);
    78.         Spostamento[67] = new Vector3(0.00154f, 0, 0.0005489f);
    79.         Spostamento[68] = new Vector3(0.00146f, 0, 0.0004196f);
    80.         Spostamento[69] = new Vector3(0.00135f, 0, 0.0002722f);
    81.         Spostamento[70] = new Vector3(0.0012f, 0, 0.0001213f);
    82.        
    83.  
    84.         StartCoroutine(Move());
    85.    
    86.     IEnumerator Move()
    87.     {
    88.             for (int i = 0; i < value; i++)
    89.             {
    90.                 transform.position = Spostamento[i];
    91.               yield return new WaitForSeconds(0.01f);
    92.              }
    93.     }
    94.         StopCoroutine(Move());
    95.  
    96.     }
    97. }
    98.  
    Hi This is my script that simulate earthquake in my structure.
    It's too long then this with 5000 raw but i post only 71.

    Next my step is lunch this script when i select an object or a button in my scene.
    How can i do?
    I try with button that active my game Object and then script, but it's incorrect because my script run when scene is start

    Thanks for all reply and help me
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    The above code is quite confused. Your Move() coroutine is located inside of your Start() function. I don't think you want that. Put it outside the Start() function.

    ALSO, if you don't want it to happen immediately, don't start the coroutine in the Start() function! Instead make a button responder function and start it there.

    ALSO, look up tutorials for getting a shake... you do NOT want to have 5000 Vector3 objects stored, that's silly for just vibration motion. There's lots of tutorials for camera shake. Just apply one of those to your building shake, unless you really care about a specific pre-made motion.

    And if you want a very specific pre-made motion, just make an animation and start that in code. Let Unity do the hard work!
     
  3. simone_fratoni

    simone_fratoni

    Joined:
    Jan 17, 2020
    Posts:
    13
    Thanks for Reply. I'm engineering student and my structure must be in that point (x,y,z) in that time. This point are specific from another engeneering software that give me every 0.1 s a new x,y,z position and with animation i put manually every point that's no good for 50 second. if reduce point effect it's valid.

    For launch my scene i don't want with a click but with a button in my scene that allow movement