Search Unity

Best script to get Acceleration from a gameobject?

Discussion in 'Physics' started by SickBoyRulez, Sep 15, 2021.

  1. SickBoyRulez

    SickBoyRulez

    Joined:
    Mar 30, 2021
    Posts:
    3
    Hi developers, I'm working on a project and I need to know the acceleration from a gameobject, which is the best way? I've find some different scripts:

    1:
    Code (CSharp):
    1. acceleration = (rigidbody.velocity - lastVelocity) / Time.fixedDeltaTime;
    2. lastVelocity = rigidbody.velocity;
    2:
    Code (CSharp):
    1.  
    2.  public Vector3 acceleration;
    3.     public Vector3 distancemoved=Vector3.zero;
    4.     public Vector3 lastdistancemoved=Vector3.zero;
    5.     public Vector3 last = transform.position;
    6.  void Update(){  
    7.         distancemoved = (transform.position - last) * Time.deltaTime ;
    8.         acceleration = distancemoved - lastdistancemoved;
    9.         lastdistancemoved = distancemoved;
    10.         last = transform.position;
    11.     }
    12.  
    3:
    Code (CSharp):
    1. //This function calculates the acceleration vector in meter/second^2.
    2.     //Input: position. If the output is used for motion simulation, the input transform
    3.     //has to be located at the seat base, not at the vehicle CG. Attach an empty GameObject
    4.     //at the correct location and use that as the input for this function.
    5.     //Gravity is not taken into account but this can be added to the output if needed.
    6.     //A low number of samples can give a jittery result due to rounding errors.
    7.     //If more samples are used, the output is more smooth but has a higher latency.
    8.     public static bool LinearAcceleration(out Vector3 vector, Vector3 position, int samples){
    9.         Vector3 averageSpeedChange = Vector3.zero;
    10.         vector = Vector3.zero;
    11.         Vector3 deltaDistance;
    12.         float deltaTime;
    13.         Vector3 speedA;
    14.         Vector3 speedB;
    15.         //Clamp sample amount. In order to calculate acceleration we need at least 2 changes
    16.         //in speed, so we need at least 3 position samples.
    17.         if(samples < 3){
    18.             samples = 3;
    19.         }
    20.         //Initialize
    21.         if(positionRegister == null){
    22.             positionRegister = new Vector3[samples];
    23.             posTimeRegister = new float[samples];
    24.         }
    25.         //Fill the position and time sample array and shift the location in the array to the left
    26.         //each time a new sample is taken. This way index 0 will always hold the oldest sample and the
    27.         //highest index will always hold the newest sample.
    28.         for(int i = 0; i < positionRegister.Length - 1; i++){
    29.             positionRegister[i] = positionRegister[i+1];
    30.             posTimeRegister[i] = posTimeRegister[i+1];
    31.         }
    32.         positionRegister[positionRegister.Length - 1] = position;
    33.         posTimeRegister[posTimeRegister.Length - 1] = Time.time;
    34.         positionSamplesTaken++;
    35.         //The output acceleration can only be calculated if enough samples are taken.
    36.         if(positionSamplesTaken >= samples){
    37.             //Calculate average speed change.
    38.             for(int i = 0; i < positionRegister.Length - 2; i++){
    39.                 deltaDistance = positionRegister[i+1] - positionRegister[i];
    40.                 deltaTime = posTimeRegister[i+1] - posTimeRegister[i];
    41.                 //If deltaTime is 0, the output is invalid.
    42.                 if(deltaTime == 0){
    43.                     return false;
    44.                 }
    45.                 speedA = deltaDistance / deltaTime;
    46.                 deltaDistance = positionRegister[i+2] - positionRegister[i+1];
    47.                 deltaTime = posTimeRegister[i+2] - posTimeRegister[i+1];
    48.                 if(deltaTime == 0){
    49.                     return false;
    50.                 }
    51.                 speedB = deltaDistance / deltaTime;
    52.                 //This is the accumulated speed change at this stage, not the average yet.
    53.                 averageSpeedChange += speedB - speedA;              
    54.             }
    55.             //Now this is the average speed change.
    56.             averageSpeedChange /= positionRegister.Length - 2;
    57.             //Get the total time difference.
    58.             float deltaTimeTotal = posTimeRegister[posTimeRegister.Length - 1] - posTimeRegister[0];          
    59.             //Now calculate the acceleration, which is an average over the amount of samples taken.
    60.             vector = averageSpeedChange / deltaTimeTotal;
    61.             return true;      
    62.         }
    63.         else{
    64.             return false;
    65.         }
    66.     }
     
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    The first one has less code, so is faster and simpler.
     
  3. nymios

    nymios

    Joined:
    Jul 31, 2023
    Posts:
    1
    This post is old but:
    first one only works if the object has a rigidbody attached to it.
    second one doesnt calculate acceleration but average speed between points
    third one seems long at first but looks best to me if the transform position of the object is used