Advertisement
Guest User

Untitled

a guest
Aug 26th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.54 KB | None | 0 0
  1. /// ---------------------------------------------
  2. /// Ultimate Character Controller
  3. /// Copyright (c) Opsive. All Rights Reserved.
  4. /// https://www.opsive.com
  5. /// ---------------------------------------------
  6.  
  7. namespace Opsive.UltimateCharacterController.Integrations.RealisticCarController
  8. {
  9.     using Opsive.Shared.Game;
  10.     using Opsive.UltimateCharacterController.Camera;
  11.     using Opsive.UltimateCharacterController.Character;
  12.     using Opsive.UltimateCharacterController.Objects.CharacterAssist;
  13.     using UnityEngine;
  14.     using System.Collections;
  15.     using Photon.Pun;
  16.     using Opsive.UltimateCharacterController.Networking;
  17.     using Photon.Pun.UtilityScripts;
  18.     using Photon.Realtime;
  19.  
  20.     /// <summary>
  21.     /// Implements IDriveSource for a Realistic Car Controller vehicle.
  22.     /// </summary>
  23.     [RequireComponent(typeof(PhotonView))]
  24.     public class PUN_RCCDriveSource : MonoBehaviourPunCallbacks, IDriveSource
  25.     {
  26.         [Tooltip("The location that the character drives from.")]
  27.         [SerializeField] protected Transform m_DriverLocation;
  28.         [Tooltip("The unique Animator identifier of the vehicle. See the drive documentation for more information.")]
  29.         [SerializeField] protected int m_AnimatorID;
  30.         [Tooltip("When the character is driving should the RCC Camera Controller be used?")]
  31.         [SerializeField] protected bool m_UseCarCameraController = true;
  32.  
  33.         private GameObject m_GameObject;
  34.         private Transform m_Transform;
  35.         private Collider[] m_IgnoreColliders;
  36.  
  37.         private RCC_CarControllerV3 m_CarController;
  38.         private RCC_Camera m_Camera;
  39.         private GameObject m_Character;
  40.         private bool m_AutoFocus = true;
  41.  
  42.         public GameObject GameObject { get => m_GameObject; }
  43.         public Transform Transform { get => m_Transform; }
  44.         public Transform DriverLocation { get => m_DriverLocation; }
  45.         public int AnimatorID { get => m_AnimatorID; }
  46.  
  47.         private PhotonView m_PhotonView;
  48.  
  49.         private INetworkInfo m_NetworkInfo;
  50.  
  51.         private void Awake()
  52.         {
  53.             m_Camera = FindObjectOfType<RCC_Camera>();
  54.             Debug.Log(m_Camera);
  55.         }
  56.         /// <summary>
  57.         /// Initialize the default values.
  58.         /// </summary>
  59.         private void Start()
  60.         {
  61.             m_PhotonView = GetComponent<PhotonView>();
  62.             m_GameObject = gameObject;
  63.             m_Transform = transform;
  64.             m_NetworkInfo = m_GameObject.GetCachedComponent<INetworkInfo>();
  65.             StartCoroutine(GetColliders());
  66.  
  67.             m_CarController = m_GameObject.GetComponent<RCC_CarControllerV3>();
  68.             if (m_CarController == null)
  69.             {
  70.                 Debug.LogError("Error: The RCCDriveSource must be attached to a vehicle with the RCC_CarController attached to it.");
  71.                 return;
  72.             }
  73.  
  74.             EnableDisableVehicle(false);
  75.         }
  76.  
  77.         IEnumerator GetColliders()
  78.         {
  79.             yield return new WaitForFixedUpdate();
  80.             m_IgnoreColliders = m_GameObject.GetComponentsInChildren<Collider>();
  81.         }
  82.  
  83.         /// <summary>
  84.         /// Enables or disables the RCC components.
  85.         /// </summary>
  86.         /// <param name="enable">Should the vehicle be enabled?</param>
  87.         private void EnableDisableVehicle(bool enable)
  88.         {
  89.             if (m_CarController == null)
  90.             {
  91.                 return;
  92.             }
  93.  
  94.             if (!enable)
  95.                 m_CarController.rigid.velocity = new Vector3(0f, 0f, 0f);
  96.  
  97.             m_CarController.enabled = enable;
  98.             if (m_UseCarCameraController && m_Camera != null)
  99.             {
  100.                 if (m_Character != null)
  101.                 {
  102.                     // Edy's Vehicle Camera Controller can take control of the camera.
  103.                     var cameraController = m_Character.GetCachedComponent<UltimateCharacterLocomotion>().LookSource as CameraController;
  104.                     if (cameraController != null)
  105.                     {
  106.                         cameraController.gameObject.SetActive(!enable);
  107.                     }
  108.                 }
  109.                 m_Camera.playerCar = enable ? m_CarController : null;
  110.                 // Set the AutoFocus so RCC doesn't start a coroutine when the camera is disabled.
  111.                 if (!enable)
  112.                 {
  113.                     m_AutoFocus = m_Camera.TPSAutoFocus;
  114.                     m_Camera.TPSAutoFocus = false;
  115.                 }
  116.                 else
  117.                 {
  118.                     m_Camera.TPSAutoFocus = m_AutoFocus;
  119.                 }
  120.                 m_Camera.gameObject.SetActive(enable);
  121.             }
  122.         }
  123.  
  124.         /// <summary>
  125.         /// The character has started to enter the vehicle.
  126.         /// </summary>
  127.         /// <param name="character">The character that is entering the vehicle.</param>
  128.         public void EnterVehicle(GameObject character)
  129.         {
  130.             if (!m_PhotonView.IsMine && character.GetComponent<PhotonView>().IsMine)
  131.                 m_PhotonView.RequestOwnership();
  132.  
  133.             var characterLocomotion = character.GetCachedComponent<UltimateCharacterLocomotion>();
  134.  
  135.             for (int i = 0; i < m_IgnoreColliders.Length; ++i)
  136.             {
  137.                 for (int j = 0; j < characterLocomotion.ColliderCount; ++j)
  138.                 {
  139.                     if (m_IgnoreColliders[i] != null)
  140.                         Physics.IgnoreCollision(m_IgnoreColliders[i], characterLocomotion.Colliders[j], true);
  141.                 }
  142.             }
  143.  
  144.             characterLocomotion.AddIgnoredColliders(m_IgnoreColliders);
  145.             m_PhotonView.RPC(nameof(EnterVehicleRPC), RpcTarget.Others);
  146.             Debug.Log("Entered car");
  147.         }
  148.  
  149.         /// <summary>
  150.         /// The character has entered the vehicle.
  151.         /// </summary>
  152.         /// <param name="character">The character that entered the vehicle.</param>
  153.         public void EnteredVehicle(GameObject character)
  154.         {
  155.             Application.targetFrameRate = 60;
  156.             var characterLocomotion = character.GetCachedComponent<UltimateCharacterLocomotion>();
  157.             characterLocomotion.UpdateLocation = Game.KinematicObjectManager.UpdateLocation.Update;
  158.             m_Character = character;
  159.             EnableDisableVehicle(true);
  160.         }
  161.  
  162.  
  163.         [PunRPC]
  164.         public void EnterVehicleRPC()
  165.         {
  166.             Debug.Log("Entered RCC RPC");
  167.             GetComponent<Rigidbody>().isKinematic = true;
  168.             m_CarController.enabled = true;
  169.         }
  170.  
  171.         [PunRPC]
  172.         public void ExitVehicleRPC()
  173.         {
  174.             Debug.Log("Exited RCC RPC");
  175.             GetComponent<Rigidbody>().isKinematic = false;
  176.             m_CarController.enabled = false;
  177.         }
  178.  
  179.         /// <summary>
  180.         /// The character has started to exit the vehicle.
  181.         /// </summary>
  182.         /// <param name="character">The character that is exiting the vehicle.</param>
  183.         public void ExitVehicle(GameObject character)
  184.         {
  185.             var characterLocomotion = character.GetCachedComponent<UltimateCharacterLocomotion>();
  186.             characterLocomotion.UpdateLocation = Game.KinematicObjectManager.UpdateLocation.FixedUpdate;
  187.             EnableDisableVehicle(false);
  188.             m_Character = null;
  189.         }
  190.  
  191.         /// <summary>
  192.         /// The character has exited the vehicle.
  193.         /// </summary>
  194.         /// <param name="character">The character that exited the vehicle.</param>
  195.         public void ExitedVehicle(GameObject character)
  196.         {
  197.             var characterLocomotion = character.GetCachedComponent<UltimateCharacterLocomotion>();
  198.             characterLocomotion.RemoveIgnoredColliders(m_IgnoreColliders);
  199.             for (int i = 0; i < m_IgnoreColliders.Length; ++i)
  200.             {
  201.                 for (int j = 0; j < characterLocomotion.ColliderCount; ++j)
  202.                 {
  203.                     if (m_IgnoreColliders[i] != null)
  204.                         Physics.IgnoreCollision(m_IgnoreColliders[i], characterLocomotion.Colliders[j], false);
  205.                 }
  206.             }
  207.             m_PhotonView.RPC(nameof(ExitVehicleRPC), RpcTarget.Others);
  208.         }
  209.  
  210.         public override void OnPlayerEnteredRoom(Player newPlayer)
  211.         {
  212.             base.OnPlayerEnteredRoom(newPlayer);
  213.             if(photonView.IsMine && m_Character != null)
  214.             {
  215.                 Debug.Log("New player connected. Sending RPC to sync state in car.");
  216.                 m_PhotonView.RPC(nameof(EnterVehicleRPC), newPlayer);
  217.             }
  218.         }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement