Advertisement
Guest User

BallPhysics.cs

a guest
Jan 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class BallPhysics : MonoBehaviour {
  4. private float ballSpeed; // скорость шарика
  5. Rigidbody rb; // rigidbody объекта
  6. private bool up; // летишь вверх или вниз
  7. private bool right; // то, в какую сторону ты летишь в 0.3 секунды бездействия
  8. private float timer1; // таймер, отвечающий за кулдаун после удара
  9. public Vector3 HorizontalDirection, VerticalDirection;
  10. public float timer3; // таймер, отвечающий за кулдаун разрушения блоков
  11. public bool iced; // пока не используется
  12. public static bool Dead; // если игрок мёртв, игнорируем всю физику
  13.  
  14. void Start () {
  15. Dead = false;
  16. rb = GetComponent<Rigidbody>();
  17. }
  18.  
  19.  
  20. void Update () {
  21. if (Input.GetKey (KeyCode.Space) || Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift))
  22. ballSpeed = 13.5319148936f;
  23. else
  24. ballSpeed = 6.76595744682f;
  25. if (!Dead) {
  26. if (timer1 == 0) { // все дальнейшие проверки происходят только вне 0.3 секунд кулдауна
  27. if (!iced) { // работает всегда, так как iced не используется
  28. if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))
  29. HorizontalDirection = new Vector3 (-0.3f, 0, 0);
  30. if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))
  31. HorizontalDirection = new Vector3 (0.3f, 0, 0);
  32. VerticalDirection = new Vector3(0.0f, up ? ballSpeed : -ballSpeed, 0.0f);
  33. rb.velocity = VerticalDirection + HorizontalDirection;
  34. }
  35. } else {
  36. // после 0.3 секунд игроку возвращается контроль над шаром
  37. if (timer1 > 0)
  38. timer1 -= Time.deltaTime;
  39. if (timer1 < 0) {
  40. timer1 = 0;
  41. }
  42. if (timer1 > 0.3f)
  43. timer1 = 0.3f;
  44. }
  45. } else {
  46. rb.velocity = Vector3.zero; // если игрок мёртв, то он остаётся на месте
  47. }
  48. if(timer3>0){
  49. timer3-=Time.deltaTime;
  50. } else {
  51. timer3=0;
  52. }
  53. if(timer3>0.01f){
  54. timer3=0.01f;
  55. }
  56. }
  57.  
  58.  
  59.  
  60. void OnCollisionEnter(Collision col){
  61. if (col.collider.CompareTag("Cube")) {
  62. if((Mathf.Abs(col.transform.GetComponent<Transform>().position.x-GetComponent<Transform>().position.x)< 2.151f)) {
  63. if (timer1 > 0)
  64. rb.velocity = (new Vector3 (right ? 6.0f : -6.0f, (ballSpeed > 7) ? (up ? -12 : 12) : (up ? -6 : 6), 0));
  65. up = col.transform.position.y <= transform.position.y;
  66. } else {
  67. timer1 = 0.3f;
  68. right = col.transform.GetComponent<Transform>().position.x <= GetComponent<Transform>().position.x;
  69. rb.velocity = (new Vector3 (right ? 6.0f : -6.0f, (ballSpeed > 7) ? (up ? 12 : -12) : (up ? 6 : -6), 0));
  70. }
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement