EgonMilanVotrubec

Transform_vs_Physics

Oct 14th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using UnityEngine;
  5.  
  6. public class Runner : MonoBehaviour
  7. {
  8.     public Vector3 StartPosition = new Vector3 ( 10, 0, 0 );
  9.     public Vector3 EndPosition = new Vector3 ( -10, 0, 0 );
  10.  
  11.  
  12.     // Each Unity unit is considered a metre.
  13.     public float MetresPerSecond;
  14.  
  15.     [Tooltip("The Prefab without a RigidBody")]
  16.     public GameObject TransformPrefab;
  17.  
  18.     [Tooltip ( "The Prefab with a RigidBody" )]
  19.     public GameObject RigidBodyPrefab;
  20.  
  21.     public int [ ] NumberToInstantiate = new int [ ] { 10_000, 20_000, 30_000 };
  22.  
  23.     private List<GameObject> TransformObjects;
  24.     private List<Rigidbody> RigidBodyObjects;
  25.  
  26.     private float timer = 0;
  27.  
  28.     private float distanceToTravel;
  29.     private float velocityReciprocal;
  30.     private bool started = false;
  31.     private bool finished = false;
  32.     private int frameCount = 0;
  33.     private int frameCountFixed = 0;
  34.     private bool fixedFinished;
  35.     private float startTime;
  36.     Stopwatch stopwatch = new Stopwatch ( );
  37.  
  38.     private int currentlyTransformTest = 1;
  39.     private int currentInstantiationIndex = 0;
  40.     private string [ ] testName = new string[] { "RigidBody", "Transform" };
  41.  
  42.     private float xPosition;
  43.  
  44.     void Start()
  45.     {
  46.  
  47.         distanceToTravel = ( StartPosition - EndPosition ).magnitude;
  48.         velocityReciprocal = 1 / ( distanceToTravel / MetresPerSecond );
  49.  
  50.         Setup ( );
  51.     }
  52.  
  53.     void Setup (  )
  54.     {
  55.         started = finished = fixedFinished = false;
  56.         timer = 0;
  57.  
  58.         // Instantiate both of the lists of objects.
  59.         TransformObjects = new List<GameObject> ( NumberToInstantiate [ currentInstantiationIndex ] );
  60.         RigidBodyObjects = new List<Rigidbody> ( NumberToInstantiate [ currentInstantiationIndex ] );
  61.  
  62.         for ( int i = 0; i < NumberToInstantiate [ currentInstantiationIndex ]; ++i )
  63.         {
  64.             TransformObjects.Add ( Instantiate ( TransformPrefab, StartPosition, Quaternion.identity ) );
  65.             RigidBodyObjects.Add ( Instantiate ( RigidBodyPrefab, StartPosition, Quaternion.identity ).GetComponent<Rigidbody> ( ) );
  66.             RigidBodyObjects [ i ].velocity = ( EndPosition - StartPosition ) * velocityReciprocal;
  67.             RigidBodyObjects [ i ].gameObject.SetActive ( false );
  68.         }
  69.     }
  70.  
  71.  
  72.     void Reset ( )
  73.     {
  74.         started = finished = fixedFinished = false;
  75.         timer = 0;
  76.  
  77.         // Flip the test.
  78.         currentlyTransformTest = 1 - currentlyTransformTest;
  79.  
  80.         if ( currentlyTransformTest == 1 )
  81.         {
  82.             // Destroy the objects in both lists.
  83.             for ( int i = 0; i < NumberToInstantiate [ currentInstantiationIndex ]; ++i )
  84.             {
  85.                 Destroy ( TransformObjects [ i ] );
  86.                 Destroy ( RigidBodyObjects [ i ].gameObject );
  87.             }
  88.  
  89.             // Increase the currentInstantiationIndex
  90.             currentInstantiationIndex++;
  91.             if ( currentInstantiationIndex >= NumberToInstantiate.Length ) currentInstantiationIndex = 0;
  92.  
  93.             // Now Setup the lists again.
  94.             Setup ( );
  95.             return;
  96.         }
  97.         else
  98.         {
  99.             for ( int i = 0; i < NumberToInstantiate [ currentInstantiationIndex ]; ++i )
  100.                 RigidBodyObjects [ i ].gameObject.SetActive ( true );
  101.         }
  102.     }
  103.  
  104.  
  105.     void Update()
  106.     {
  107.         if ( finished ) return;
  108.         if ( !started )
  109.         {
  110.             startTime = Time.time;
  111.             stopwatch.Restart ( );
  112.             stopwatch.Start ( );
  113.             started = true;
  114.             frameCount = frameCountFixed = 0;
  115.         }
  116.  
  117.         frameCount++;
  118.         timer += (Time.deltaTime * velocityReciprocal);
  119.  
  120.         if ( currentlyTransformTest == 1 )
  121.         {
  122.             for ( int i = 0; i < NumberToInstantiate [ currentInstantiationIndex ]; ++i )
  123.                 TransformObjects [ i ].transform.localPosition = Vector3.Lerp ( StartPosition, EndPosition, timer );
  124.             xPosition = TransformObjects [ 0 ].transform.localPosition.x;
  125.         }
  126.         else
  127.             xPosition = RigidBodyObjects [ 0 ].transform.localPosition.x;
  128.  
  129.         // We can cheat here, because we know that the object is moving right to left, along the x axis.
  130.         if ( xPosition <= EndPosition.x )
  131.         {
  132.             stopwatch.Stop ( );
  133.             finished = true;
  134.             UnityEngine.Debug.Log ( $"Finished running using {testName[currentlyTransformTest]} test. {NumberToInstantiate [ currentInstantiationIndex ]} Objects.\n" +
  135.                 $"Elapsed Time : { Time.time - startTime }/{ stopwatch.Elapsed.TotalSeconds } seconds (Unity/StopWatch)." +
  136.                 $" Frames : {frameCount}. Frame Rate : { frameCount / ( Time.time - startTime ) }/{ frameCount / stopwatch.Elapsed.TotalSeconds }" );
  137.             Reset ( );
  138.         }
  139.     }
  140.  
  141.     private void FixedUpdate ( )
  142.     {
  143.         if ( finished || !started || fixedFinished ) return;
  144.         frameCountFixed++;
  145.         if ( currentlyTransformTest == 0 )
  146.             xPosition = RigidBodyObjects [ 0 ].transform.localPosition.x;
  147.         else
  148.             xPosition = TransformObjects [ 0 ].transform.localPosition.x;
  149.        
  150.         if ( xPosition <= EndPosition.x )
  151.         {
  152.             fixedFinished = true;
  153.             UnityEngine.Debug.Log ( $"FixedUpdate {testName [ currentlyTransformTest ]} crossed the line at { Time.time - startTime }/{ stopwatch.Elapsed.TotalSeconds }" +
  154.                 $" seconds (Unity/StopWatch)!\nFrame {frameCount} and Fixed Frame {frameCountFixed}." );
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment