/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Demo.Car
{
using Opsive.Shared.Events;
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Demo.UnityStandardAssets.Vehicles.Car;
using Opsive.UltimateCharacterController.Objects.CharacterAssist;
using UnityEngine;
/// <summary>
/// Provides a sample implementation of the IDriveSource.
/// </summary>
public class SkyCarCopy : MonoBehaviour, IDriveSource
{
[Tooltip("A reference to the colliders that should be disabled when the character enters the car.")]
[UnityEngine.Serialization.FormerlySerializedAs("m_Colliders")]
[SerializeField] protected GameObject[] m_DisableColliders;
[Tooltip("The location that the character drives from.")]
[SerializeField] protected Transform m_DriverLocation;
//rcc
[Tooltip("When the character is driving should the RCC Camera Controller be used?")]
[SerializeField] protected bool m_UseCarCameraController = true;
//
private static int s_OpenCloseDoorParameter = Animator.StringToHash("OpenCloseDoor");
private GameObject m_GameObject;
private Transform m_Transform;
private Animator m_Animator;
private Rigidbody m_Rigidbody;
private Collider[] m_IgnoreColliders;
private CarUserControl m_UserControl;
private AnimatorMonitor m_CharacterAnimatorMonitor;
private int m_HorizontalInputID;
private bool m_OpenedDoor;
//rcc
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 => 0; }
/// <summary>
/// Initialize the default values.
/// </summary>
private void Start() // went with Start because that is what RCC used. Have to find out Whats the diff
{
m_GameObject = gameObject;
m_Transform = transform;
m_Animator = GetComponent<Animator>();
m_Rigidbody = GetComponent<Rigidbody>();
m_IgnoreColliders = m_GameObject.GetComponentsInChildren<Collider>();
//ucc
//m_UserControl = GetComponent<CarUserControl>();
m_HorizontalInputID = Animator.StringToHash("HorizontalInput");
//rcc
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;
}
m_Camera = FindObjectOfType<RCC_Camera>();
//Removed and swapped because RCC needed the items in EnableDisableVehicle
// EnableDisableCar(false);
EnableDisableVehicle(false);
}
/*
* Removed because it only deals with the things taken care of by RCC
private void EnableDisableCar(bool enable)
{
enabled = m_UserControl.enabled = m_Audio.enabled = enable;
m_Rigidbody.isKinematic = !enable;
for (int i = 0; i < m_Headlights.Length; ++i) {
m_Headlights[i].SetActive(enable);
}
for (int i = 0; i < m_DisableColliders.Length; ++i) {
m_DisableColliders[i].SetActive(!enable);
}
}
*/
/// <summary>
/// Enables or disables the car components.
/// </summary>
/// <param name="enable">Should the car be enabled?</param>
private void EnableDisableVehicle(bool enable)
{
Debug.Log("Enable/Disable Run");
//rcc
m_CarController.enabled = enable;
if (m_UseCarCameraController && m_Camera != null)
{
if (m_Character != null)
{
// RCC'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);
}
//UCC Skycar: This Automatically disabled all colliders on the car so I removed it
//m_Rigidbody.isKinematic = !enable;
/*
for (int i = 0; i < m_DisableColliders.Length; ++i)
{
m_DisableColliders[i].SetActive(!enable);
}
if (m_CarController == null)
{
return;
}
*/
}
/// <summary>
/// The character has started to enter the vehicle.
/// </summary>
/// <param name="character">The character that is entering the vehicle.</param>
///
/// Identicle code except Skycar Registers for event "OnAnimatorOpenCloseDoor" sent by Animation
public void EnterVehicle(GameObject character)
{
Debug.Log("EnterVehicle Run");
EventHandler.RegisterEvent(character, "OnAnimatorOpenCloseDoor", OpenCloseDoor);
var characterLocomotion = character.GetCachedComponent<UltimateCharacterLocomotion>();
for (int i = 0; i < m_IgnoreColliders.Length; ++i)
{
for (int j = 0; j < characterLocomotion.ColliderCount; ++j)
{
Physics.IgnoreCollision(m_IgnoreColliders[i], characterLocomotion.Colliders[j], true);
}
}
characterLocomotion.AddIgnoredColliders(m_IgnoreColliders);
Debug.Log("Ignoring Colliders");
}
/// <summary>
/// Triggers the OpenCloseDoor parameter. From Skycar
/// </summary>
private void OpenCloseDoor()
{
m_OpenedDoor = !m_OpenedDoor;
m_Animator.SetTrigger(s_OpenCloseDoorParameter);
}
/// <summary>
/// The character has entered the vehicle.
/// </summary>
/// <param name="character">The character that entered the vehicle.</param>
public void EnteredVehicle(GameObject character)
{
m_CharacterAnimatorMonitor = character.GetCachedComponent<AnimatorMonitor>(); //Skycar code
m_Character = character; //RCC Code
EnableDisableVehicle(true); //RCC
}
/// <summary>
/// Updates the animator. From Skycar
/// Errors out if enabled on start and RCC must be enabled on start. Will figure later how to fix error
/// </summary>
public void Update()
{
m_Animator.SetFloat(m_HorizontalInputID, m_CharacterAnimatorMonitor.AbilityFloatData, 0, 0);
}
/// <summary>
/// The character has started to exit the vehicle. Mostly RCC after removing EnableDisableCar
/// </summary>
/// <param name="character">The character that is exiting the vehicle.</param>
public void ExitVehicle(GameObject character)
{
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)
{
EventHandler.UnregisterEvent(character, "OnAnimatorOpenCloseDoor", OpenCloseDoor); //Unregistering for Event
//Mirrored in both RCC and Skycar
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)
{
Physics.IgnoreCollision(m_IgnoreColliders[i], characterLocomotion.Colliders[j], false);
}
}
//Skycar runs door close animation on vehicle
if (m_OpenedDoor)
{
OpenCloseDoor();
}
}
}
}