Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.36 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public class CarMovement : MonoBehaviour {
  6.  
  7. public int m_PlayerNumber; // Lo usamos para identificar el coche del jugador
  8. public float m_Speed = 0f; // Como de rapido se desplaza el coche
  9. private float max_speed = 0f; //Velocitat màxima variable (Segons el joystick)
  10. private float current_speed = 0f; //Auxiliar per controlar la velocitat del cotxe
  11. private float relative_speed;
  12. public float m_TurnSpeed = 180f; // Como de rapido gira el coche en grados por segundo
  13. /* public Transform m_FrontLeftWheel; //Referencia a la rueda frontal izquierda
  14. public Transform m_FrontRightWheel; //Referencia a la rueda frontal derecha
  15. public Transform m_ChasisRotation; //Referencia al chasis del coche */
  16. private float maxSteerAngle = 7; //Angulo maximo de giro de las ruedas
  17. private float maxLeenAngle= 5; //Angulo maximo de inclinacion del chasis
  18. public bool isGoingForward;
  19. public Vector3 carDirection;
  20. private float acceleracio = 0f; //Valor inicial de aceleracion
  21.  
  22. private string m_MovementAxisName; // The name of the input axis for moving forward and back.
  23. private string m_TurnAxisName; // The name of the input axis for turning.
  24. private string m_MovementAxisNameJoystic; // The name of the input axis for moving forward and back.
  25. private string m_TurnAxisNameJoystic; // The name of the input axis for turning.
  26. private Rigidbody m_Rigidbody; // Reference used to move the tank.
  27. private float m_MovementInputValue; // The current value of the movement input.
  28. private float m_TurnInputValue; // The current value of the turn input.
  29.  
  30.  
  31. private bool powerup = false;
  32. private bool isScaled = false;
  33. public CarMovement enemyMovement;
  34. private bool hasMine = false;
  35.  
  36. private bool loadFlag = false;
  37.  
  38.  
  39. //Buttons
  40. String horizontal;
  41. String A;
  42. String B;
  43. String triggers;
  44.  
  45. private void Awake ()
  46. {
  47. m_Rigidbody = GetComponent<Rigidbody> ();
  48. m_Rigidbody.rotation = Quaternion.Euler(0f, 0f, 0f);
  49. }
  50.  
  51.  
  52. private void OnEnable ()
  53. {
  54. // When the car is turned on, make sure it's not kinematic.
  55. m_Rigidbody.isKinematic = false;
  56.  
  57. // Also reset the input values.
  58. m_MovementInputValue = 0f;
  59. m_TurnInputValue = 0f;
  60. }
  61.  
  62.  
  63. private void OnDisable ()
  64. {
  65. // When the tank is turned off, set it to kinematic so it stops moving.
  66. m_Rigidbody.isKinematic = true;
  67. }
  68.  
  69.  
  70. private void Start ()
  71. {
  72. mapButtons();
  73. if (GlobalManager.manager.player1Controller == 0) {
  74. // KEYBOARD
  75. m_MovementAxisName = "Vertical" + m_PlayerNumber;
  76. m_TurnAxisName = "Horizontal" + m_PlayerNumber;
  77. } else {
  78. //JOYSTICK
  79. m_MovementAxisNameJoystic = triggers;
  80. m_TurnAxisNameJoystic = horizontal;
  81. }
  82.  
  83. if (GlobalManager.manager.player2Controller == 0) {
  84. // KEYBOARD
  85. m_MovementAxisName = "Vertical" + m_PlayerNumber;
  86. m_TurnAxisName = "Horizontal" + m_PlayerNumber;
  87. } else {
  88. //JOYSTICK
  89. m_MovementAxisNameJoystic = triggers;
  90. m_TurnAxisNameJoystic = horizontal;
  91. }
  92.  
  93. carDirection = transform.forward;
  94.  
  95.  
  96. }
  97.  
  98.  
  99. private void Update ()
  100. {
  101. //Comprovo que els cotxes s'hagin carregat.
  102. if(GameManager.sceneLoaded && !loadFlag){
  103. GameObject child = this.transform.GetChild(0).gameObject;
  104. m_Speed = child.GetComponentsInChildren<carConfig>()[0].m_Speed; //Accedim a les variables de propietats del child.
  105. loadFlag = true;
  106. }
  107.  
  108.  
  109. int controller1Plugged = GlobalManager.manager.player1Controller;
  110. int controller2Plugged = GlobalManager.manager.player2Controller;
  111. //Este control necessita de 2 if para contemplar el caso de
  112. if(m_PlayerNumber == 1){
  113. if (controller1Plugged == 1) {
  114. m_MovementInputValue = Input.GetAxisRaw (m_MovementAxisNameJoystic);
  115. m_TurnInputValue = Input.GetAxis (m_TurnAxisNameJoystic);
  116. } else {
  117. m_MovementInputValue = Input.GetAxisRaw (m_MovementAxisName);
  118. m_TurnInputValue = Input.GetAxis (m_TurnAxisName);
  119. }
  120. }
  121. if(m_PlayerNumber == 2 ){
  122. // Store the value of both input axes.
  123. if (controller2Plugged == 1) {
  124. m_MovementInputValue = Input.GetAxisRaw (m_MovementAxisNameJoystic);
  125. m_TurnInputValue = Input.GetAxis (m_TurnAxisNameJoystic);
  126. } else {
  127. m_MovementInputValue = Input.GetAxisRaw (m_MovementAxisName);
  128. m_TurnInputValue = Input.GetAxis (m_TurnAxisName);
  129. }
  130. }
  131. carDirection = transform.forward;
  132. carDirection.y = 0f;
  133.  
  134. if (Input.GetKeyDown ("space")) {
  135. //Debug.Log ("Space Button pressed");
  136. if (this.hasMine) {
  137. // Debug.Log ("Throw mine");
  138.  
  139. }
  140. }
  141. //Debug.Log ("TRIGGERS= "+m_MovementInputValue);
  142. //Debug.Log ("JOYSTICK= "+m_TurnInputValue);
  143. }
  144.  
  145. private void FixedUpdate ()
  146. {
  147. // Adjust the rigidbodies position and orientation in FixedUpdate.
  148. mapButtons();
  149. Move ();
  150. Turn ();
  151. }
  152.  
  153.  
  154. private void Move ()
  155.  
  156. {
  157. max_speed = m_MovementInputValue*m_Speed;
  158. if(max_speed<0){
  159. max_speed*=-1; //max_speed és en mòdul, no ens informa de la direccio. SEMPRE POSITIU!
  160. }
  161.  
  162. /*
  163. Si el joystick està activat, la velocitat del cotxe es modificarà per aproximar-se a la del joystick.
  164. En cas de que sigui superior (ja sigui positiva o negativa), el cotxe frenarà, per això s'envia al else.
  165. */
  166. if (m_MovementInputValue != 0 && Math.Abs(current_speed) <= max_speed) {
  167.  
  168. //Calculo l'acceleració segons
  169. if(m_MovementInputValue>0){
  170. acceleracio = 0.5f;
  171. }else{
  172. acceleracio = -0.5f;
  173. }
  174.  
  175. current_speed += acceleracio;
  176.  
  177. if(m_MovementInputValue>0){
  178. if(current_speed > max_speed){
  179. current_speed = max_speed;
  180. }
  181. }else if(m_MovementInputValue<0){
  182. if(current_speed < -max_speed){
  183. current_speed = -max_speed;
  184. }
  185. }
  186.  
  187. //Limito velocitat
  188. if(current_speed >= max_speed){
  189. current_speed = max_speed;
  190. }else if(current_speed <= -max_speed){
  191. current_speed = -max_speed;
  192. }
  193.  
  194. } else {
  195. current_speed*=0.95f;
  196. if(current_speed < 0.001f && current_speed > -0.001f){
  197. current_speed = 0;
  198. }
  199. }
  200.  
  201.  
  202.  
  203. // Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
  204. Vector3 movement = transform.forward * Time.deltaTime * current_speed;
  205. // Apply this movement to the rigidbody's position.
  206. m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
  207.  
  208. //Variable usada a Hegagon.cs
  209. if (current_speed >= 0) {
  210. isGoingForward = true;
  211. } else {
  212. isGoingForward = false;
  213. }
  214.  
  215. //Variable que s'utlititza a Turn().
  216. if(m_Speed!=0){
  217. relative_speed = current_speed / m_Speed;
  218. }else{
  219. relative_speed = 0;
  220. }
  221.  
  222. }
  223.  
  224. private void Turn ()
  225. {
  226. //Valor que l'usuari vol girar. Gir màxim proporcional al joystick.
  227. float input_turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
  228. //Modifiquem el valor de gir segons la velocitat.
  229. float turn = input_turn * turnCapacity();
  230. Vector3 eulerAngleVelocity = new Vector3(0.0f, turn, 0.0f);
  231. //Fem girar el cotxe
  232. Quaternion turnRotation = Quaternion.Euler (eulerAngleVelocity);
  233. m_Rigidbody.MoveRotation (m_Rigidbody.rotation * turnRotation );
  234. //Debug.Log(m_Rigidbody.rotation.eulerAngles);
  235.  
  236. }
  237.  
  238. /*
  239. Aquesta funció ajuda a calcular el gir que pot arribar a fer un cotxe segons la seva velocitat.
  240. Fins un cert valor 'optim' és proporcional i a partir de llavors és inversament proporcional, sense
  241. arribar a 0 (el mínim és aux_max) El if és per retornar els valors de la "funció"
  242. En definitiva, la millor velocitat per girar és optim*m_Speed
  243. */
  244. private float turnCapacity(){
  245. float optim = 0.5f; // Rang = (+0, 1)
  246. float aux_max = 0.5f; //Rang = (+0, 1]
  247. if(relative_speed <= optim){
  248. return relative_speed / optim;
  249. }else{
  250. return relative_speed*(1-aux_max)/(1-optim);
  251. }
  252. }
  253.  
  254. void OnTriggerEnter(Collider other)
  255. {
  256. if (other.gameObject.CompareTag ("Pickup") )
  257. {
  258. other.gameObject.SetActive (false);
  259. switch (other.gameObject.name) {
  260. case "Freeze(Clone)":
  261. Debug.Log (this.name);
  262. if (this.name == "SpawnerPlayer2") {
  263. enemyMovement = GameObject.FindGameObjectWithTag ("Player1").GetComponent<CarMovement>();
  264. //enemyMovement = GameObject.Find ("Mustang1").GetComponent<CarMovement> ();
  265. enemyMovement.m_Speed = 0.0f;
  266. enemyMovement.m_TurnSpeed = 0.0f;
  267. enemyMovement.Invoke("SetNormalSpeed",3.5f);
  268. } else {
  269. enemyMovement = GameObject.FindGameObjectWithTag ("Player2").GetComponent<CarMovement>();
  270. //enemyMovement = GameObject.Find ("Mustang2").GetComponent<CarMovement> ();
  271. enemyMovement.m_Speed = 0.0f;
  272. enemyMovement.m_TurnSpeed = 0.0f;
  273. enemyMovement.Invoke("SetNormalSpeed",3.5f);
  274. }
  275.  
  276. break;
  277. case "Turbo(Clone)":
  278. //Debug.Log ("Entro a Turbo");
  279. this.m_Speed = 20.0f;
  280. Invoke ("SetNormalSpeed", 5);
  281. break;
  282. case "2xWide(Clone)":
  283. //Debug.Log ("Scale" + transform.localScale);
  284. if (!isScaled) {
  285. isScaled = true;
  286. transform.localScale += new Vector3 (0.01f, 0.01f, 0.01f);
  287. Invoke ("SetNormalScale", 5);
  288. }
  289. break;
  290.  
  291. case "Sphere(Clone)":
  292. this.hasMine = true;
  293. break;
  294. default:
  295. break;
  296.  
  297. }
  298. }
  299. }
  300.  
  301. public void SetNormalSpeed(){
  302. this.m_Speed = 12.0f;
  303. this.m_TurnSpeed = 180.0f;
  304. }
  305. public void SetNormalScale(){
  306. transform.localScale -= new Vector3 (0.01f, 0.01f, 0.01f);
  307. isScaled = false;
  308. }
  309.  
  310. void mapButtons(){
  311. if (m_PlayerNumber == 1) {
  312. switch (GlobalManager.manager.controllerName1) {
  313. case "XBOX":
  314. horizontal = "XBOX_Horizontal" + m_PlayerNumber;
  315. A = "XBOX_A"+ m_PlayerNumber;
  316. B = "XBOX_B"+ m_PlayerNumber;
  317. triggers="XBOX_Triggers"+ m_PlayerNumber;
  318. break;
  319. case "PS4":
  320. horizontal = "PS4_Horizontal" + m_PlayerNumber;
  321. A = "PS4_A"+ m_PlayerNumber;
  322. B = "PS4_B"+ m_PlayerNumber;
  323. triggers="PS4_Triggers"+ m_PlayerNumber;
  324. break;
  325. }
  326. }
  327. if (m_PlayerNumber == 2) {
  328. switch (GlobalManager.manager.controllerName2) {
  329. case "XBOX":
  330. horizontal = "XBOX_Horizontal" + m_PlayerNumber;
  331. A = "XBOX_A"+ m_PlayerNumber;
  332. B = "XBOX_B"+ m_PlayerNumber;
  333. triggers="XBOX_Triggers"+ m_PlayerNumber;
  334. break;
  335. case "PS4":
  336. horizontal = "PS4_Horizontal" + m_PlayerNumber;
  337. A = "PS4_A"+ m_PlayerNumber;
  338. B = "PS4_B"+ m_PlayerNumber;
  339. triggers="PS4_Triggers"+ m_PlayerNumber;
  340. break;
  341. }
  342. }
  343.  
  344. }
  345.  
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement