Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using Photon.Pun;
- using Controls; //this is a library of control scripts I made
- using Photon.Pun.UtilityScripts;
- public class Player : MonoBehaviourPunCallbacks
- {
- public CameraController cameraController;
- public Camera cameraGameObject;
- public VariableJoystick variableJoystick;
- [Tooltip("The local player instance. Use this to know if the local player is represented in the Scene")]
- public static GameObject LocalPlayerInstance;
- private new PhotonView photonView;
- [SerializeField]
- public GameObject playerUIPrefab;
- public void Awake()
- {
- photonView = GetComponent<PhotonView>();
- playerRigidBody = this.GetComponent<Rigidbody>();
- if (cameraGameObject != null)
- {
- cameraController = cameraGameObject.GetComponent<CameraController>();
- }
- // used in GameManager.cs: we keep track of the localPlayer instance to prevent instanciation when levels are synchronized
- if (photonView.IsMine)
- {
- LocalPlayerInstance = gameObject;
- }
- // Destroy item if doesn't belong to this view
- if (!photonView.IsMine && GetComponent<VariableJoystick>() != null)
- {
- Destroy(GetComponent<VariableJoystick>());
- Destroy(playerUIPrefab);
- }
- // we flag as don't destroy on load so that instance survives level synchronization, thus giving a seamless experience when levels load.
- DontDestroyOnLoad(gameObject);
- }
- public void Start()
- {
- // Dobbiamo reistanziare la UI se cambia scena, questo perché in PlayerUI la distruggiamo
- // se il giocatore target viene distrutto (cosa che accade quando cambia scena).
- #if UNITY_5_4_OR_NEWER
- // Unity 5.4 has a new scene management. register a method to call CalledOnLevelWasLoaded.
- UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
- #endif
- }
- public override void OnDisable()
- {
- // Always call the base to remove callbacks
- base.OnDisable();
- #if UNITY_5_4_OR_NEWER
- UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded;
- #endif
- }
- void FixedUpdate()
- {
- if (!photonView.IsMine)
- {
- return;
- }
- if (photonView.IsMine)
- {
- //rolling ball
- if (playerRigidBody.GetComponentInChildren<Animator>().GetInteger("Comportamento") == (int)BehaviourCode.ROLLING && isGrounded)
- {
- MovementModes.rollingMode(playerRigidBody, maxSpeed, maxTurboSpeed, accelerationForce, turboForce, brakeForce, zeroSpeedCorrection, variableJoystick, buttonA_on, steeringCorrection);
- }
- //increased fall speed
- if (!isGrounded)
- {
- PhysicsEvents.increaseGravity(playerRigidBody, gravityIncreaseWhileFalling);
- }
- //rotate camera
- if (playerRigidBody.GetComponentInChildren<Animator>().GetInteger("Comportamento") == (int)BehaviourCode.ROOTED
- && cameraController.rotateCameraOn && Input.touchCount == 1)
- {
- cameraController.fingerRotateCamera();
- }
- //zoom camera
- if (playerRigidBody.GetComponentInChildren<Animator>().GetInteger("Comportamento") == (int)BehaviourCode.ROOTED
- && Input.touchCount == 2 && cameraController.rotateCameraOn)
- {
- cameraController.pinchZoomCamera();
- }
- //set camera default on rolling
- if (playerRigidBody.GetComponentInChildren<Animator>().GetInteger("Comportamento") == (int)BehaviourCode.ROLLING
- && cameraGameObject.GetComponent<Camera>().fieldOfView > cameraController.defaultCameraFieldOfView)
- {
- cameraController.zoomCamera(-cameraResetSpeed);
- }
- //set camera default on rooted
- else if (playerRigidBody.GetComponentInChildren<Animator>().GetInteger("Comportamento") == (int)BehaviourCode.ROOTED
- && cameraGameObject.GetComponent<Camera>().fieldOfView < cameraController.maxFieldOfView && !cameraController.pinchCameraOn)
- {
- cameraController.zoomCamera(cameraResetSpeed);
- if (!cameraController.pinchCameraOn && cameraGameObject.GetComponent<Camera>().fieldOfView >= cameraController.maxFieldOfView)
- cameraController.pinchCameraOn = true;
- }
- }
- }
- void CalledOnLevelWasLoaded(int level)
- {
- // check if we are outside the Arena and if it's the case, spawn around the center of the arena in a safe zone
- if (!Physics.Raycast(transform.position, -Vector3.up, 5f))
- {
- transform.position = new Vector3(0f, 5f, 0f);
- }
- }
- #if UNITY_5_4_OR_NEWER
- void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode loadingMode)
- {
- this.CalledOnLevelWasLoaded(scene.buildIndex);
- }
- #endif
Add Comment
Please, Sign In to add comment