Advertisement
teleias

Unity3D Collision Detector + Timer

Dec 17th, 2013
1,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. //Apparently a Unity3D code snippet that times how long it takes for A to collide with B
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5.  
  6. public class CollisionDetector : MonoBehaviour {
  7.  
  8.     //A new variable named timer.
  9.     // Contains the timing class I made.
  10.     Timer timer;
  11.    
  12.     //This begins the timing process.
  13.     void Start () {
  14.         timer = new Timer();
  15.     }
  16.        
  17.     //OnGUI is called every frame.
  18.     // This just makes a box that shows the time.
  19.     // You can delete this and use whatever.
  20.     void OnGUI () {
  21.         //Only try to get the time if the timer isn't null.
  22.         // If it is null, we can't get time from it
  23.         //  because it doesn't exist!
  24.         if(timer != null)
  25.         {
  26.             //Make a new GUI Box.
  27.             // Inside, show the time (which is an integer)
  28.             // We add +"" to make it a string.
  29.             // It's the same as .toString()
  30.             GUILayout.Box(timer.getTime()+"");
  31.         }
  32.     }
  33.    
  34.     //We're going to start the timer once we move
  35.     // And NOT before.
  36.     //This is a simple true/false variable to store that.
  37.     bool moved = false;
  38.    
  39.     //Called every frame (basically a loop).
  40.     // In this function, we just want to wait for a move
  41.     //  To be made. Then we start the timer, and don't use this
  42.     //   function ever again.
  43.     void Update()
  44.     {
  45.         //Did we press the left/right arrow keys?
  46.         // If so, we record that data into [delta].
  47.         // That time is multiplied by [Time.deltaTime].
  48.         // This just normalizes the data.
  49.         // Without it, the [delta] variable would be dependant on how
  50.         // quick the computer's frame rate is.
  51.         float delta = Input.GetAxis("Horizontal")*Time.deltaTime;
  52.         //Translate (move) this transform (the gameObject) by the amount.
  53.         // We go to the right, multiplied by the [delta].
  54.         // Note the delta can be negative or positive.
  55.         transform.Translate(Vector3.right*delta);
  56.         //If we haven't moved in any of the past frames BUT
  57.         // the current frame has some (non-zero) movement,
  58.         // then that means we have moved.
  59.         if(moved == false && delta != 0)
  60.         {
  61.             //Set [moved] to true
  62.             // This means the above line (moved == false)
  63.             // will forevermore be untrue. (true == false) -> false
  64.             // We don't need to check if we moved again anyways.
  65.             moved = true;
  66.             //Start the timer by using timer.startTimer().
  67.             timer.startTimer();
  68.         }
  69.     }
  70.    
  71.     //On trigger enter.
  72.     //
  73.     //Setup in Editor:
  74.     //
  75.     //@Moving Object
  76.     // Add this class to the [moving object].
  77.     //  Add a rigidbody to that object.
  78.     //   Make sure {Gravity} = false.
  79.     //   and {Kinematic} = false.
  80.     //  Use a regular collider.
  81.     //@End GameObject
  82.     // Make sure the [end object] has a collider.
  83.     //  Set [end]'s {trigger} = true.
  84.     //
  85.     // This setup makes [movingObj] collide with walls and stuff (as usual)
  86.     // However, the endObject is a trigger, which means stuff can move through it,
  87.     //  but an event is still sent.
  88.     //
  89.     // When the [movingObj] hits the trigger, OnTriggerEnter is called.
  90.     // The parameter, [other], is the collider script of the end object.
  91.     void OnTriggerEnter(Collider other)
  92.     {
  93.         //Stops the timer.
  94.         // This might get called multiple times
  95.         //  if the triggers hit over and over, but it will be ignored
  96.         //  by the timer code.
  97.         timer.stopTimer();
  98.     }
  99.    
  100.     //This is a class I just randomly made myself.
  101.     // Its probably worse than the one you gave me, but oh well~
  102.     public class Timer
  103.     {
  104.         int startTime;
  105.         int stopTime;
  106.         bool isRunning = false;
  107.         public Timer(){}
  108.         public int getTime()
  109.         {
  110.             if(isRunning)
  111.             {
  112.                 return (int)Time.time-startTime;
  113.             }
  114.             else
  115.             {
  116.                 return stopTime-startTime;
  117.             }
  118.         }
  119.         public void startTimer()
  120.         {
  121.             startTime = (int)Time.time;
  122.             isRunning = true;
  123.             Debug.Log("Timer Start");
  124.         }
  125.         public void stopTimer()
  126.         {
  127.             if(isRunning)
  128.             {
  129.                 stopTime = (int)Time.time;
  130.                 isRunning = false;
  131.                 Debug.Log("Stopped Timer at "+getTime()+" seconds.");
  132.             }
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement