irishstorm

CollisionDetection.js

Dec 12th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  CollisionDetection.js
  2. //  Date : 05/12/2012
  3. //  Time : 09:58
  4. //  Author : Christopher Cullen
  5.  
  6. //  Description :
  7. //  This function handles the collision between objects.
  8.  
  9. //  Declares the Variable "hitCount" as a Static variable of a type Int with a value of "0".
  10. static var hitCount : int = 0;
  11.  
  12. //  function Update()
  13. //  This resets the objects X and Y position.
  14. //  not the best way to do this but it is the only way i can get collision to work
  15. //  without adding a ground plane.
  16. /*
  17. function Update()
  18. {
  19.     transform.position.x = 0;
  20.     transform.position.y = 0;
  21. }
  22. */
  23.  
  24. //  function OnCollisionEnter()
  25. //  This function handles the collision.
  26. //  If an object collides with this objects collider it will
  27. //  count how ment times it has collided with the object.
  28.  
  29. //  Problems
  30. //  1)  Hit's don't register unless one of the objects have a rigidbody.
  31. //      possible fix's would be to set the X and Y position to zero on
  32. //      very frame, but that wouldn't be ideal.
  33.  
  34. function OnCollisionEnter(hit : Collision)
  35. {
  36.     if(hit.gameObject.tag == "Player")  
  37.     {
  38.         hitCount += 1;
  39.         DoForce(200);
  40.     }
  41. }
  42.  
  43. //  function DoForce()
  44. //  This function handles the force applied to the object.
  45. function DoForce(force : float )
  46. {
  47.     transform.rigidbody.AddForce(transform.position * force);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment