Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
3.5 kB
2
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Invector.vCharacterController;

public class CarEnterExit : MonoBehaviour
{
    public GameObject player; // The player object
    public GameObject car; // The car object
    public Transform carSeat; // The transform of the seat in the car where the player will be seated
    public bool inCar = false; // Flag to track whether the player is in the car (set to false by default)

    private vThirdPersonInput playerController; // The CharacterController component on the player object
    private CarController carController; // The CarController component on the car object

    void Start()
    {
        // Get the CharacterController component on the player object
        playerController = player.GetComponent<vThirdPersonInput>();
        // Get the CarController component on the car object
        carController = car.GetComponent<CarController>();
    }

    void Update()
    {
        // If the player is in the car and press the "E" key, exit the car
        if (inCar && Input.GetKeyDown(KeyCode.E))
        {
            ExitCar();
        }
        // If the player is not in the car and press the "E" key, try to enter the car
        else if (!inCar && Input.GetKeyDown(KeyCode.E))
        {
            EnterCar();
        }
    }

    void OnTriggerEnter(Collider other)
    {
        // If the player collides with the car, show the "Press E to enter" prompt
        if (other.gameObject == player)
        {
            Debug.Log("Press E to enter car");
        }
    }

    void OnTriggerExit(Collider other)
    {
        // If the player exits the car's trigger zone, hide the "Press E to enter" prompt
        if (other.gameObject == player)
        {
            Debug.Log("");
        }
    }

    void EnterCar()
    {
        // Enable the CharacterController component on the player object
        playerController.enabled = false;
        // Set the player's parent to the car
        player.transform.parent = car.transform;
        // Set the player's position to the car seat
        player.transform.position = carSeat.position;
        // Set the player's rotation to match the car seat's rotation
        player.transform.rotation = carSeat.rotation;
        // Set the player's flag to indicate that they are in the car
        inCar = true;
        // Enable the CarController component on the car object
        carController.enabled = true;
        // Disable the player's mesh renderer
        //player.GetComponentInChildren<MeshRenderer>().enabled = false;
    }

    void ExitCar()
    {
        // Disable the CarController component on the car object
        carController.enabled = false;
        // Set the player's parent to the default parent (null)
        player.transform.parent = null;
        // Set the player's position to the position of the car
        player.transform.position = car.transform.position;
        // Set the player's rotation to match the car's rotation
        player.transform.rotation = car.transform.rotation;
        // Set the player's flag to indicate that they are not in the car
        inCar = false;
        // Enable the CharacterController component on the player object
        playerController.enabled = true;
        // Enable the player's mesh renderer
        //player.GetComponentInChildren<MeshRenderer>().enabled = true;
    }
}