Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityStandardAssets.CrossPlatformInput;
  5.  
  6. public class Mouvement : MonoBehaviour
  7. {
  8. Rigidbody rb;
  9. private bool canMove = true;
  10.  
  11. public bool rotate = false;
  12. private int lastRotation = 0;
  13.  
  14.  
  15. public float speed = 1f;
  16.  
  17.  
  18. private void Start()
  19. {
  20. rb = GetComponent<Rigidbody>();
  21.  
  22. }
  23.  
  24.  
  25. //Buttons
  26.  
  27. public void RotateButton(bool right)
  28. {
  29. if (!canMove)
  30. return;
  31. float temp = 90;
  32. if (!right)
  33. temp = -90;
  34.  
  35. StartCoroutine(RotateMe(Vector3.up * temp, 0.5f, right));
  36.  
  37. }
  38.  
  39. IEnumerator RotateMe(Vector3 byAngles, float inTime, bool right)
  40. {
  41. canMove = false;
  42. var fromAngle = transform.rotation;
  43. var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
  44. for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
  45. {
  46. transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
  47. yield return null;
  48. }
  49.  
  50. transform.eulerAngles = new Vector3(0, CheckRotationAngle(right), 0);
  51. canMove = true;
  52. }
  53.  
  54. private float CheckRotationAngle(bool right)
  55. {
  56. int temp = 0;
  57. switch (lastRotation)
  58. {
  59.  
  60. case 0:
  61. if (right)
  62. temp = 90;
  63. else
  64. temp = -90;
  65. break;
  66. case 90:
  67. if (right)
  68. temp = 180;
  69. else
  70. temp = 0;
  71. break;
  72. case 180:
  73. if (right)
  74. temp = -90;
  75. else
  76. temp = 90;
  77. break;
  78. case -90:
  79. if (right)
  80. temp = 0;
  81. else
  82. temp = -180;
  83. break;
  84. case -180:
  85. if (right)
  86. temp = -90;
  87. else
  88. temp = 90;
  89. break;
  90. }
  91. lastRotation = temp;
  92. return (float)temp;
  93. }
  94.  
  95. private void FixedUpdate()
  96. {
  97. if (!GameInfo.CanMove)
  98. {
  99. rb.velocity = Vector3.zero;
  100. return;
  101. }
  102.  
  103. float translation = CrossPlatformInputManager.GetAxis("Vertical") * speed;
  104. float straffe = CrossPlatformInputManager.GetAxis("Horizontal") * speed;
  105.  
  106. translation *= Time.deltaTime;
  107. straffe *= Time.deltaTime;
  108.  
  109. transform.Translate(straffe, 0, translation);
  110.  
  111. if(straffe + translation == 0)
  112. {
  113. if(rb.velocity != Vector3.zero)
  114. rb.velocity = Vector3.zero;
  115. }
  116. }
  117.  
  118. public bool GetCanMove()
  119. {
  120. return canMove;
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement