Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- public class CarMovement : MonoBehaviour {
- public int m_PlayerNumber; // Lo usamos para identificar el coche del jugador
- public float m_Speed = 0f; // Como de rapido se desplaza el coche
- private float max_speed = 0f; //Velocitat màxima variable (Segons el joystick)
- private float current_speed = 0f; //Auxiliar per controlar la velocitat del cotxe
- private float relative_speed;
- public float m_TurnSpeed = 180f; // Como de rapido gira el coche en grados por segundo
- /* public Transform m_FrontLeftWheel; //Referencia a la rueda frontal izquierda
- public Transform m_FrontRightWheel; //Referencia a la rueda frontal derecha
- public Transform m_ChasisRotation; //Referencia al chasis del coche */
- private float maxSteerAngle = 7; //Angulo maximo de giro de las ruedas
- private float maxLeenAngle= 5; //Angulo maximo de inclinacion del chasis
- public bool isGoingForward;
- public Vector3 carDirection;
- private float acceleracio = 0f; //Valor inicial de aceleracion
- private string m_MovementAxisName; // The name of the input axis for moving forward and back.
- private string m_TurnAxisName; // The name of the input axis for turning.
- private string m_MovementAxisNameJoystic; // The name of the input axis for moving forward and back.
- private string m_TurnAxisNameJoystic; // The name of the input axis for turning.
- private Rigidbody m_Rigidbody; // Reference used to move the tank.
- private float m_MovementInputValue; // The current value of the movement input.
- private float m_TurnInputValue; // The current value of the turn input.
- private bool powerup = false;
- private bool isScaled = false;
- public CarMovement enemyMovement;
- private bool hasMine = false;
- private bool loadFlag = false;
- //Buttons
- String horizontal;
- String A;
- String B;
- String triggers;
- private void Awake ()
- {
- m_Rigidbody = GetComponent<Rigidbody> ();
- m_Rigidbody.rotation = Quaternion.Euler(0f, 0f, 0f);
- }
- private void OnEnable ()
- {
- // When the car is turned on, make sure it's not kinematic.
- m_Rigidbody.isKinematic = false;
- // Also reset the input values.
- m_MovementInputValue = 0f;
- m_TurnInputValue = 0f;
- }
- private void OnDisable ()
- {
- // When the tank is turned off, set it to kinematic so it stops moving.
- m_Rigidbody.isKinematic = true;
- }
- private void Start ()
- {
- mapButtons();
- if (GlobalManager.manager.player1Controller == 0) {
- // KEYBOARD
- m_MovementAxisName = "Vertical" + m_PlayerNumber;
- m_TurnAxisName = "Horizontal" + m_PlayerNumber;
- } else {
- //JOYSTICK
- m_MovementAxisNameJoystic = triggers;
- m_TurnAxisNameJoystic = horizontal;
- }
- if (GlobalManager.manager.player2Controller == 0) {
- // KEYBOARD
- m_MovementAxisName = "Vertical" + m_PlayerNumber;
- m_TurnAxisName = "Horizontal" + m_PlayerNumber;
- } else {
- //JOYSTICK
- m_MovementAxisNameJoystic = triggers;
- m_TurnAxisNameJoystic = horizontal;
- }
- carDirection = transform.forward;
- }
- private void Update ()
- {
- //Comprovo que els cotxes s'hagin carregat.
- if(GameManager.sceneLoaded && !loadFlag){
- GameObject child = this.transform.GetChild(0).gameObject;
- m_Speed = child.GetComponentsInChildren<carConfig>()[0].m_Speed; //Accedim a les variables de propietats del child.
- loadFlag = true;
- }
- int controller1Plugged = GlobalManager.manager.player1Controller;
- int controller2Plugged = GlobalManager.manager.player2Controller;
- //Este control necessita de 2 if para contemplar el caso de
- if(m_PlayerNumber == 1){
- if (controller1Plugged == 1) {
- m_MovementInputValue = Input.GetAxisRaw (m_MovementAxisNameJoystic);
- m_TurnInputValue = Input.GetAxis (m_TurnAxisNameJoystic);
- } else {
- m_MovementInputValue = Input.GetAxisRaw (m_MovementAxisName);
- m_TurnInputValue = Input.GetAxis (m_TurnAxisName);
- }
- }
- if(m_PlayerNumber == 2 ){
- // Store the value of both input axes.
- if (controller2Plugged == 1) {
- m_MovementInputValue = Input.GetAxisRaw (m_MovementAxisNameJoystic);
- m_TurnInputValue = Input.GetAxis (m_TurnAxisNameJoystic);
- } else {
- m_MovementInputValue = Input.GetAxisRaw (m_MovementAxisName);
- m_TurnInputValue = Input.GetAxis (m_TurnAxisName);
- }
- }
- carDirection = transform.forward;
- carDirection.y = 0f;
- if (Input.GetKeyDown ("space")) {
- //Debug.Log ("Space Button pressed");
- if (this.hasMine) {
- // Debug.Log ("Throw mine");
- }
- }
- //Debug.Log ("TRIGGERS= "+m_MovementInputValue);
- //Debug.Log ("JOYSTICK= "+m_TurnInputValue);
- }
- private void FixedUpdate ()
- {
- // Adjust the rigidbodies position and orientation in FixedUpdate.
- mapButtons();
- Move ();
- Turn ();
- }
- private void Move ()
- {
- max_speed = m_MovementInputValue*m_Speed;
- if(max_speed<0){
- max_speed*=-1; //max_speed és en mòdul, no ens informa de la direccio. SEMPRE POSITIU!
- }
- /*
- Si el joystick està activat, la velocitat del cotxe es modificarà per aproximar-se a la del joystick.
- En cas de que sigui superior (ja sigui positiva o negativa), el cotxe frenarà, per això s'envia al else.
- */
- if (m_MovementInputValue != 0 && Math.Abs(current_speed) <= max_speed) {
- //Calculo l'acceleració segons
- if(m_MovementInputValue>0){
- acceleracio = 0.5f;
- }else{
- acceleracio = -0.5f;
- }
- current_speed += acceleracio;
- if(m_MovementInputValue>0){
- if(current_speed > max_speed){
- current_speed = max_speed;
- }
- }else if(m_MovementInputValue<0){
- if(current_speed < -max_speed){
- current_speed = -max_speed;
- }
- }
- //Limito velocitat
- if(current_speed >= max_speed){
- current_speed = max_speed;
- }else if(current_speed <= -max_speed){
- current_speed = -max_speed;
- }
- } else {
- current_speed*=0.95f;
- if(current_speed < 0.001f && current_speed > -0.001f){
- current_speed = 0;
- }
- }
- // Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
- Vector3 movement = transform.forward * Time.deltaTime * current_speed;
- // Apply this movement to the rigidbody's position.
- m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
- //Variable usada a Hegagon.cs
- if (current_speed >= 0) {
- isGoingForward = true;
- } else {
- isGoingForward = false;
- }
- //Variable que s'utlititza a Turn().
- if(m_Speed!=0){
- relative_speed = current_speed / m_Speed;
- }else{
- relative_speed = 0;
- }
- }
- private void Turn ()
- {
- //Valor que l'usuari vol girar. Gir màxim proporcional al joystick.
- float input_turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
- //Modifiquem el valor de gir segons la velocitat.
- float turn = input_turn * turnCapacity();
- Vector3 eulerAngleVelocity = new Vector3(0.0f, turn, 0.0f);
- //Fem girar el cotxe
- Quaternion turnRotation = Quaternion.Euler (eulerAngleVelocity);
- m_Rigidbody.MoveRotation (m_Rigidbody.rotation * turnRotation );
- //Debug.Log(m_Rigidbody.rotation.eulerAngles);
- }
- /*
- Aquesta funció ajuda a calcular el gir que pot arribar a fer un cotxe segons la seva velocitat.
- Fins un cert valor 'optim' és proporcional i a partir de llavors és inversament proporcional, sense
- arribar a 0 (el mínim és aux_max) El if és per retornar els valors de la "funció"
- En definitiva, la millor velocitat per girar és optim*m_Speed
- */
- private float turnCapacity(){
- float optim = 0.5f; // Rang = (+0, 1)
- float aux_max = 0.5f; //Rang = (+0, 1]
- if(relative_speed <= optim){
- return relative_speed / optim;
- }else{
- return relative_speed*(1-aux_max)/(1-optim);
- }
- }
- void OnTriggerEnter(Collider other)
- {
- if (other.gameObject.CompareTag ("Pickup") )
- {
- other.gameObject.SetActive (false);
- switch (other.gameObject.name) {
- case "Freeze(Clone)":
- Debug.Log (this.name);
- if (this.name == "SpawnerPlayer2") {
- enemyMovement = GameObject.FindGameObjectWithTag ("Player1").GetComponent<CarMovement>();
- //enemyMovement = GameObject.Find ("Mustang1").GetComponent<CarMovement> ();
- enemyMovement.m_Speed = 0.0f;
- enemyMovement.m_TurnSpeed = 0.0f;
- enemyMovement.Invoke("SetNormalSpeed",3.5f);
- } else {
- enemyMovement = GameObject.FindGameObjectWithTag ("Player2").GetComponent<CarMovement>();
- //enemyMovement = GameObject.Find ("Mustang2").GetComponent<CarMovement> ();
- enemyMovement.m_Speed = 0.0f;
- enemyMovement.m_TurnSpeed = 0.0f;
- enemyMovement.Invoke("SetNormalSpeed",3.5f);
- }
- break;
- case "Turbo(Clone)":
- //Debug.Log ("Entro a Turbo");
- this.m_Speed = 20.0f;
- Invoke ("SetNormalSpeed", 5);
- break;
- case "2xWide(Clone)":
- //Debug.Log ("Scale" + transform.localScale);
- if (!isScaled) {
- isScaled = true;
- transform.localScale += new Vector3 (0.01f, 0.01f, 0.01f);
- Invoke ("SetNormalScale", 5);
- }
- break;
- case "Sphere(Clone)":
- this.hasMine = true;
- break;
- default:
- break;
- }
- }
- }
- public void SetNormalSpeed(){
- this.m_Speed = 12.0f;
- this.m_TurnSpeed = 180.0f;
- }
- public void SetNormalScale(){
- transform.localScale -= new Vector3 (0.01f, 0.01f, 0.01f);
- isScaled = false;
- }
- void mapButtons(){
- if (m_PlayerNumber == 1) {
- switch (GlobalManager.manager.controllerName1) {
- case "XBOX":
- horizontal = "XBOX_Horizontal" + m_PlayerNumber;
- A = "XBOX_A"+ m_PlayerNumber;
- B = "XBOX_B"+ m_PlayerNumber;
- triggers="XBOX_Triggers"+ m_PlayerNumber;
- break;
- case "PS4":
- horizontal = "PS4_Horizontal" + m_PlayerNumber;
- A = "PS4_A"+ m_PlayerNumber;
- B = "PS4_B"+ m_PlayerNumber;
- triggers="PS4_Triggers"+ m_PlayerNumber;
- break;
- }
- }
- if (m_PlayerNumber == 2) {
- switch (GlobalManager.manager.controllerName2) {
- case "XBOX":
- horizontal = "XBOX_Horizontal" + m_PlayerNumber;
- A = "XBOX_A"+ m_PlayerNumber;
- B = "XBOX_B"+ m_PlayerNumber;
- triggers="XBOX_Triggers"+ m_PlayerNumber;
- break;
- case "PS4":
- horizontal = "PS4_Horizontal" + m_PlayerNumber;
- A = "PS4_A"+ m_PlayerNumber;
- B = "PS4_B"+ m_PlayerNumber;
- triggers="PS4_Triggers"+ m_PlayerNumber;
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement