shredingskin
New member
Hello, I've been looking at the integration script, and the problem is that the way the script is enabling/disabling car controlling is being done by just deactivating the script.
Here's how I changed the script to make it work better:
Hope this helps (there's a lot of room for improvement, RCC has a ton of stuff).
Here's how I changed the script to make it work better:
C#:
namespace Opsive.UltimateCharacterController.Integrations.RealisticCarController
{
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Camera;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Objects.CharacterAssist;
using UnityEngine;
/// <summary>
/// Implements IDriveSource for a Realistic Car Controller vehicle.
/// </summary>
public class RCCDriveSource : MonoBehaviour, 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_SceneManager m_SceneManager;
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; }
/// <summary>
/// Initialize the default values.
/// </summary>
private void Start()
{
m_GameObject = gameObject;
m_Transform = transform;
m_IgnoreColliders = m_GameObject.GetComponentsInChildren<Collider>();
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_SceneManager = FindObjectOfType<RCC_SceneManager>();
m_Camera = FindObjectOfType<RCC_Camera>();
//EnableDisableVehicle(false);
m_CarController.KillEngine();
}
/// <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;
}
m_CarController.SetCanControl(enable);
//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)
{
m_SceneManager.RegisterPlayer(m_CarController);
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);
m_CarController.StartEngine();
}
/// <summary>
/// The character has entered the vehicle.
/// </summary>
/// <param name="character">The character that entered the vehicle.</param>
public void EnteredVehicle(GameObject character)
{
m_Character = character;
EnableDisableVehicle(true);
}
/// <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)
{
m_CarController.KillEngine();
m_SceneManager.DeRegisterPlayer();
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) {
Physics.IgnoreCollision(m_IgnoreColliders[i], characterLocomotion.Colliders[j], false);
}
}
}
}
}
Hope this helps (there's a lot of room for improvement, RCC has a ton of stuff).