Im making a game where the player could enter the vehicle and exit and roam around etc. now ive made a quick script where the player could enter and leave the vehicle. But it does not work with other vehicles. Example when i have 2 vehicles with the script attached to both of them and when i get in a vehicle on i can controll both of the vehicles at the same time.
Heres the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NWH.VehiclePhysics2.Input;
public class EnteringSystem : MonoBehaviour
{
public Animator TheUiAnimator;
public GameObject TheCar;
InputSystemVehicleInputProvider TheCarScript;
public GameObject SteeringWheelSupport;
public Transform PlayersPosition;
public Transform PlayersOgposition;
private GameObject Player;
private bool CanEnterCar;
public static bool InCar = false;
private CharacterController Contr;
public Animation Transition;
SmoothFpsController SmoothFpsControl;
public AudioSource GetInCarFX;
public Camera playerCamera; // Reference to the player's camera
public float interactionDistance = 5f; // Distance for the raycast to detect the car
void Start()
{
TheCarScript = TheCar.GetComponent<InputSystemVehicleInputProvider>();
Player = GameObject.FindGameObjectWithTag("Player"); // Find the player object
SmoothFpsControl = Player.GetComponent<SmoothFpsController>();
Contr = Player.GetComponent<CharacterController>();
playerCamera = Camera.main; // Assuming the main camera is the player’s camera
}
// Update is called once per frame
void Update()
{
PerformRaycast();
if (Input.GetKeyDown(KeyCode.Return))
{
if (CanEnterCar)
{
if (InCar == false) // Entering the car
{
StartCoroutine(GetInCar());
}
else // Exiting the car
{
StartCoroutine(GetOutOfCar());
}
}
}
if (MainMenu.SteeringWheelOn && InCar)
{
SteeringWheelSupport.SetActive(true);
}
else
{
SteeringWheelSupport.SetActive(false);
}
}
void PerformRaycast()
{
RaycastHit hit;
// Perform the raycast from the center of the player's camera
if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, interactionDistance))
{
// Check if we hit the car
if (hit.collider.CompareTag("Car"))
{
CanEnterCar = true;
TheUiAnimator.Play("Appear"); // Show UI for entering
}
else
{
CanEnterCar = false;
TheUiAnimator.Play("Away"); // Hide UI if not looking at the car
}
}
else
{
CanEnterCar = false;
TheUiAnimator.Play("Away"); // Hide UI if raycast doesn't hit anything
}
}
IEnumerator GetInCar()
{
Transition.Play();
GetInCarFX.Play();
yield return new WaitForSeconds(0.3f);
PlayersOgposition.position = Player.transform.position;
SmoothFpsControl.canMove = false; // Disable FPS controls while in the car
TheCarScript.enabled = true; // Enable car controls
InCar = true;
Contr.enabled = false; // Disable character controller
Player.transform.SetParent(PlayersPosition);
Player.transform.position = PlayersPosition.position; // Set player's position to car seat
TheUiAnimator.Play("Appear"); // Play UI animation for entering
}
IEnumerator GetOutOfCar()
{
Transition.Play();
GetInCarFX.Play();
yield return new WaitForSeconds(0.3f);
SmoothFpsControl.canMove = true; // Re-enable FPS controls
TheCarScript.enabled = false; // Disable car controls
InCar = false;
Player.transform.SetParent(null); // Detach player from car
Player.transform.position = PlayersOgposition.position; // Set player back to original position
Contr.enabled = true; // Re-enable character controller
TheUiAnimator.Play("Away"); // Play UI animation for exiting
}
}