Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// ---------------------------------------------
- /// Ultimate Character Controller
- /// Copyright (c) Opsive. All Rights Reserved.
- /// https://www.opsive.com
- /// ---------------------------------------------
- namespace Opsive.UltimateCharacterController.Integrations.RealisticCarController
- {
- using Opsive.Shared.Game;
- using Opsive.UltimateCharacterController.Camera;
- using Opsive.UltimateCharacterController.Character;
- using Opsive.UltimateCharacterController.Objects.CharacterAssist;
- using UnityEngine;
- using System.Collections;
- using Photon.Pun;
- using Opsive.UltimateCharacterController.Networking;
- using Photon.Pun.UtilityScripts;
- using Photon.Realtime;
- /// <summary>
- /// Implements IDriveSource for a Realistic Car Controller vehicle.
- /// </summary>
- [RequireComponent(typeof(PhotonView))]
- public class PUN_RCCDriveSource : MonoBehaviourPunCallbacks, IDriveSource
- {
- [Tooltip("The location that the character drives from.")]
- [SerializeField] protected Transform m_DriverLocation;
- [Tooltip("The unique Animator identifier of the vehicle. See the drive documentation for more information.")]
- [SerializeField] protected int m_AnimatorID;
- [Tooltip("When the character is driving should the RCC Camera Controller be used?")]
- [SerializeField] protected bool m_UseCarCameraController = true;
- private GameObject m_GameObject;
- private Transform m_Transform;
- private Collider[] m_IgnoreColliders;
- private RCC_CarControllerV3 m_CarController;
- private RCC_Camera m_Camera;
- private GameObject m_Character;
- private bool m_AutoFocus = true;
- public GameObject GameObject { get => m_GameObject; }
- public Transform Transform { get => m_Transform; }
- public Transform DriverLocation { get => m_DriverLocation; }
- public int AnimatorID { get => m_AnimatorID; }
- private PhotonView m_PhotonView;
- private INetworkInfo m_NetworkInfo;
- private void Awake()
- {
- m_Camera = FindObjectOfType<RCC_Camera>();
- Debug.Log(m_Camera);
- }
- /// <summary>
- /// Initialize the default values.
- /// </summary>
- private void Start()
- {
- m_PhotonView = GetComponent<PhotonView>();
- m_GameObject = gameObject;
- m_Transform = transform;
- m_NetworkInfo = m_GameObject.GetCachedComponent<INetworkInfo>();
- StartCoroutine(GetColliders());
- m_CarController = m_GameObject.GetComponent<RCC_CarControllerV3>();
- if (m_CarController == null)
- {
- Debug.LogError("Error: The RCCDriveSource must be attached to a vehicle with the RCC_CarController attached to it.");
- return;
- }
- EnableDisableVehicle(false);
- }
- IEnumerator GetColliders()
- {
- yield return new WaitForFixedUpdate();
- m_IgnoreColliders = m_GameObject.GetComponentsInChildren<Collider>();
- }
- /// <summary>
- /// Enables or disables the RCC components.
- /// </summary>
- /// <param name="enable">Should the vehicle be enabled?</param>
- private void EnableDisableVehicle(bool enable)
- {
- if (m_CarController == null)
- {
- return;
- }
- if (!enable)
- m_CarController.rigid.velocity = new Vector3(0f, 0f, 0f);
- m_CarController.enabled = enable;
- if (m_UseCarCameraController && m_Camera != null)
- {
- if (m_Character != null)
- {
- // Edy's Vehicle Camera Controller can take control of the camera.
- var cameraController = m_Character.GetCachedComponent<UltimateCharacterLocomotion>().LookSource as CameraController;
- if (cameraController != null)
- {
- cameraController.gameObject.SetActive(!enable);
- }
- }
- m_Camera.playerCar = enable ? m_CarController : null;
- // Set the AutoFocus so RCC doesn't start a coroutine when the camera is disabled.
- if (!enable)
- {
- m_AutoFocus = m_Camera.TPSAutoFocus;
- m_Camera.TPSAutoFocus = false;
- }
- else
- {
- m_Camera.TPSAutoFocus = m_AutoFocus;
- }
- m_Camera.gameObject.SetActive(enable);
- }
- }
- /// <summary>
- /// The character has started to enter the vehicle.
- /// </summary>
- /// <param name="character">The character that is entering the vehicle.</param>
- public void EnterVehicle(GameObject character)
- {
- if (!m_PhotonView.IsMine && character.GetComponent<PhotonView>().IsMine)
- m_PhotonView.RequestOwnership();
- var characterLocomotion = character.GetCachedComponent<UltimateCharacterLocomotion>();
- for (int i = 0; i < m_IgnoreColliders.Length; ++i)
- {
- for (int j = 0; j < characterLocomotion.ColliderCount; ++j)
- {
- if (m_IgnoreColliders[i] != null)
- Physics.IgnoreCollision(m_IgnoreColliders[i], characterLocomotion.Colliders[j], true);
- }
- }
- characterLocomotion.AddIgnoredColliders(m_IgnoreColliders);
- m_PhotonView.RPC(nameof(EnterVehicleRPC), RpcTarget.Others);
- Debug.Log("Entered car");
- }
- /// <summary>
- /// The character has entered the vehicle.
- /// </summary>
- /// <param name="character">The character that entered the vehicle.</param>
- public void EnteredVehicle(GameObject character)
- {
- Application.targetFrameRate = 60;
- var characterLocomotion = character.GetCachedComponent<UltimateCharacterLocomotion>();
- characterLocomotion.UpdateLocation = Game.KinematicObjectManager.UpdateLocation.Update;
- m_Character = character;
- EnableDisableVehicle(true);
- }
- [PunRPC]
- public void EnterVehicleRPC()
- {
- Debug.Log("Entered RCC RPC");
- GetComponent<Rigidbody>().isKinematic = true;
- m_CarController.enabled = true;
- }
- [PunRPC]
- public void ExitVehicleRPC()
- {
- Debug.Log("Exited RCC RPC");
- GetComponent<Rigidbody>().isKinematic = false;
- m_CarController.enabled = false;
- }
- /// <summary>
- /// The character has started to exit the vehicle.
- /// </summary>
- /// <param name="character">The character that is exiting the vehicle.</param>
- public void ExitVehicle(GameObject character)
- {
- var characterLocomotion = character.GetCachedComponent<UltimateCharacterLocomotion>();
- characterLocomotion.UpdateLocation = Game.KinematicObjectManager.UpdateLocation.FixedUpdate;
- EnableDisableVehicle(false);
- m_Character = null;
- }
- /// <summary>
- /// The character has exited the vehicle.
- /// </summary>
- /// <param name="character">The character that exited the vehicle.</param>
- public void ExitedVehicle(GameObject character)
- {
- var characterLocomotion = character.GetCachedComponent<UltimateCharacterLocomotion>();
- characterLocomotion.RemoveIgnoredColliders(m_IgnoreColliders);
- for (int i = 0; i < m_IgnoreColliders.Length; ++i)
- {
- for (int j = 0; j < characterLocomotion.ColliderCount; ++j)
- {
- if (m_IgnoreColliders[i] != null)
- Physics.IgnoreCollision(m_IgnoreColliders[i], characterLocomotion.Colliders[j], false);
- }
- }
- m_PhotonView.RPC(nameof(ExitVehicleRPC), RpcTarget.Others);
- }
- public override void OnPlayerEnteredRoom(Player newPlayer)
- {
- base.OnPlayerEnteredRoom(newPlayer);
- if(photonView.IsMine && m_Character != null)
- {
- Debug.Log("New player connected. Sending RPC to sync state in car.");
- m_PhotonView.RPC(nameof(EnterVehicleRPC), newPlayer);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement