Guest User

Player.cs

a guest
Apr 5th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.08 KB | None | 0 0
  1. using UnityEngine;
  2. using Photon.Pun;
  3.  
  4. using Controls; //this is a library of control scripts I made
  5. using Photon.Pun.UtilityScripts;
  6.  
  7. public class Player : MonoBehaviourPunCallbacks
  8. {
  9.     public CameraController cameraController;
  10.     public Camera cameraGameObject;
  11.     public VariableJoystick variableJoystick;
  12.  
  13.     [Tooltip("The local player instance. Use this to know if the local player is represented in the Scene")]
  14.     public static GameObject LocalPlayerInstance;
  15.  
  16.     private new PhotonView photonView;
  17.  
  18.     [SerializeField]
  19.     public GameObject playerUIPrefab;
  20.  
  21.     public void Awake()
  22.     {
  23.         photonView = GetComponent<PhotonView>();
  24.         playerRigidBody = this.GetComponent<Rigidbody>();
  25.         if (cameraGameObject != null)
  26.         {
  27.             cameraController = cameraGameObject.GetComponent<CameraController>();
  28.         }
  29.  
  30.         // used in GameManager.cs: we keep track of the localPlayer instance to prevent instanciation when levels are synchronized
  31.         if (photonView.IsMine)
  32.         {
  33.             LocalPlayerInstance = gameObject;
  34.         }
  35.  
  36.         // Destroy item if doesn't belong to this view
  37.         if (!photonView.IsMine && GetComponent<VariableJoystick>() != null)
  38.         {
  39.             Destroy(GetComponent<VariableJoystick>());
  40.             Destroy(playerUIPrefab);
  41.         }
  42.  
  43.         // we flag as don't destroy on load so that instance survives level synchronization, thus giving a seamless experience when levels load.
  44.         DontDestroyOnLoad(gameObject);
  45.     }
  46.  
  47.     public void Start()
  48.     {
  49.         // Dobbiamo reistanziare la UI se cambia scena, questo perché in PlayerUI la distruggiamo
  50.         // se il giocatore target viene distrutto (cosa che accade quando cambia scena).
  51. #if UNITY_5_4_OR_NEWER
  52.         // Unity 5.4 has a new scene management. register a method to call CalledOnLevelWasLoaded.
  53.         UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
  54. #endif
  55.  
  56.     }
  57.  
  58.     public override void OnDisable()
  59.     {
  60.         // Always call the base to remove callbacks
  61.         base.OnDisable();
  62.  
  63. #if UNITY_5_4_OR_NEWER
  64.         UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded;
  65. #endif
  66.     }
  67.  
  68.     void FixedUpdate()
  69.     {
  70.         if (!photonView.IsMine)
  71.         {
  72.             return;
  73.         }
  74.  
  75.         if (photonView.IsMine)
  76.         {
  77.             //rolling ball
  78.             if (playerRigidBody.GetComponentInChildren<Animator>().GetInteger("Comportamento") == (int)BehaviourCode.ROLLING && isGrounded)
  79.             {
  80.                 MovementModes.rollingMode(playerRigidBody, maxSpeed, maxTurboSpeed, accelerationForce, turboForce, brakeForce, zeroSpeedCorrection, variableJoystick, buttonA_on, steeringCorrection);
  81.             }
  82.  
  83.             //increased fall speed
  84.             if (!isGrounded)
  85.             {
  86.                 PhysicsEvents.increaseGravity(playerRigidBody, gravityIncreaseWhileFalling);
  87.             }
  88.  
  89.             //rotate camera
  90.             if (playerRigidBody.GetComponentInChildren<Animator>().GetInteger("Comportamento") == (int)BehaviourCode.ROOTED
  91.                 && cameraController.rotateCameraOn && Input.touchCount == 1)
  92.             {
  93.                 cameraController.fingerRotateCamera();
  94.             }
  95.  
  96.             //zoom camera
  97.             if (playerRigidBody.GetComponentInChildren<Animator>().GetInteger("Comportamento") == (int)BehaviourCode.ROOTED
  98.                 && Input.touchCount == 2 && cameraController.rotateCameraOn)
  99.             {
  100.                 cameraController.pinchZoomCamera();
  101.             }
  102.  
  103.             //set camera default on rolling
  104.             if (playerRigidBody.GetComponentInChildren<Animator>().GetInteger("Comportamento") == (int)BehaviourCode.ROLLING
  105.                 && cameraGameObject.GetComponent<Camera>().fieldOfView > cameraController.defaultCameraFieldOfView)
  106.             {
  107.                 cameraController.zoomCamera(-cameraResetSpeed);
  108.             }
  109.             //set camera default on rooted
  110.             else if (playerRigidBody.GetComponentInChildren<Animator>().GetInteger("Comportamento") == (int)BehaviourCode.ROOTED
  111.                 && cameraGameObject.GetComponent<Camera>().fieldOfView < cameraController.maxFieldOfView && !cameraController.pinchCameraOn)
  112.             {
  113.                 cameraController.zoomCamera(cameraResetSpeed);
  114.                 if (!cameraController.pinchCameraOn && cameraGameObject.GetComponent<Camera>().fieldOfView >= cameraController.maxFieldOfView)
  115.                     cameraController.pinchCameraOn = true;
  116.             }
  117.         }
  118.  
  119.     }
  120.  
  121.     void CalledOnLevelWasLoaded(int level)
  122.     {
  123.         // check if we are outside the Arena and if it's the case, spawn around the center of the arena in a safe zone
  124.         if (!Physics.Raycast(transform.position, -Vector3.up, 5f))
  125.         {
  126.             transform.position = new Vector3(0f, 5f, 0f);
  127.         }
  128.     }
  129.  
  130. #if UNITY_5_4_OR_NEWER
  131.     void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode loadingMode)
  132.     {
  133.         this.CalledOnLevelWasLoaded(scene.buildIndex);
  134.     }
  135. #endif
Add Comment
Please, Sign In to add comment