UFPS and Edy’s Vehicle Physics

Here I am showing how to use my new scripts with Edy’s Vehicle Physics along with Ultimate FPS.  This method allows you to simply add one script to the Vehicle itself which create a UFPS interactive switch.

NOTE: These scripts have been updated to work with Edy’s version 5.1 which had some awesome changes.

UFPS
Edy’s Vehicle Physics

Base Vehicle Interact Script for Inheriting

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//This is the Base EnterExit script to be put on your Vehicle.
//I am using a name space, therefore add using RedHawk; to the top
//Inherit this class - pulic class MyNewVehicleEnterExit : VehicleEnterExit
//Add variables for your car and camera to allow you to update the controls and targets
//Override any functions. For example: protected override void Start(){}
//Load the base script - such as base.Start(); inside the above override
//Add any new functionality to your override or any new functions.

namespace RedHawk
{
//Using my namespace.  If you want access, you should include using RedHawk; at the top of the script

public class VehicleInteractSwitch : vp_Interactable
{
#region Variables
KeyCode interactionKeyCode = KeyCode.F;//Re-Assigned Automatically Based on your UFPS Input Settings
public bool inVehicle = false;
[Tooltip("This is a reference to your Camera which follows the vehicle while in the vehicle.")]
public GameObject vehicleCamera = null;
[Tooltip("This is a reference to the vehicle we are looking to interact with.")]
public GameObject Target = null;
GameObject m_PlayerGameObject = null;//Assigned Automatically
[Tooltip("This is the location your Player will exit the vehicle.")]
public GameObject exitPosition = null;
[Tooltip("Use this gameObject as a Visual Representation of your Player in the Driver Seat.")]
public GameObject playerInVehicle = null;

//UFPS - Add a vp_DamageHandler to your base vehicle for this to function
vp_DamageHandler m_Handler = null;
bool hasHandler = false;
AudioSource AudioSource;
public Vector2 SwitchPitchRange = new Vector2(1.0f, 1.5f);
public List<AudioClip> SwitchSounds = new List<AudioClip>();        // list of sounds to randomly play when switched
#endregion

protected override void Start()
{
vehicleCamera.GetComponentInChildren<Camera>(true).enabled = false; //This will check the parent object plus all the children
vehicleCamera.GetComponentInChildren<AudioListener>(true).enabled = false; //This will check the parent object plus all the children
if (Target == null)
Debug.LogWarning("Assign the Vehicle via the Target element.", gameObject);
if (exitPosition == null)
Debug.LogWarning("Create a Child ExitPosition and assign it.", gameObject);
if (vehicleCamera == null)
Debug.LogWarning("Assign the Vehicle Camera.", gameObject);
if (playerInVehicle != null)
playerInVehicle.SetActive(false);
inVehicle = false;
m_Handler = Target.GetComponent<vp_DamageHandler>();
if (!m_Handler)
hasHandler = false;
else
hasHandler = true;
if (AudioSource == null)
AudioSource = GetComponent<AudioSource>() == null ? gameObject.AddComponent<AudioSource>() : GetComponent<AudioSource>();
//Automatically assign the Interact Key based on UFPS Input Manager
vp_Input.Instance.Buttons.TryGetValue("Interact", out interactionKeyCode);
}

protected virtual void FixedUpdate()
{
if (inVehicle)
{
if (Input.GetKeyDown(interactionKeyCode))
{
Action();
}
}
//UFPS  If vehicle has a vp_DamageHandler
if (hasHandler)
{
if (m_Handler.CurrentHealth <= 0f)
vehicleDie();
}
}
/// try to interact with this object
public override bool TryInteract(vp_PlayerEventHandler player)
{
//No Target - Cancel Actions
if (Target == null)
return false;
//Assign the m_Player
if (m_Player == null)
m_Player = player;

PlaySound();

Action();

return true;

}

//UFPS - Kick the player out of the vehicle before we explode
public void vehicleDie()
{
if (inVehicle)//We are in the Vehicle so do the Action and kick us out
Action();
}

//Do all Action Checks here
protected virtual void Action()
{
//Check if we are in the vehicle now
if (inVehicle)
{
GetOut();
inVehicle = false;
}
else
{
GetIn();
inVehicle = true;
}
}

//Enter Vehicle
protected virtual void GetIn()
{
m_PlayerGameObject = m_Player.gameObject;
//PLAYER - Deactivate, Make this vehicle the Parent, Move to here
m_PlayerGameObject.SetActive(false);
m_PlayerGameObject.transform.parent = Target.transform;
m_PlayerGameObject.transform.position = Target.transform.position;
//CAMERA - Enable Camera & AudioListener
vehicleCamera.GetComponent<Camera>().enabled = true;
vehicleCamera.GetComponent<AudioListener>().enabled = true;
//PLAYER IN VEHICLE - Activate visual
if (playerInVehicle)
playerInVehicle.SetActive(true);
}
//Exit Vehicle
protected virtual void GetOut()
{
m_PlayerGameObject = m_Player.gameObject;
PlaySound();
//PLAYER - Active, Remove Parent, Move to ExitPosition
m_PlayerGameObject.transform.parent = null;
m_PlayerGameObject.transform.position = exitPosition.transform.position;
m_PlayerGameObject.transform.rotation = exitPosition.transform.rotation;
m_PlayerGameObject.SetActive(true);
//CAMERA - Deactivate Camera & AudioListener
vehicleCamera.GetComponent<Camera>().enabled = false;
vehicleCamera.GetComponent<AudioListener>().enabled = false;
//PLAYER IN VEHICLE - Deactivate visual
if (playerInVehicle)
playerInVehicle.SetActive(false);
}
//COPIED DIRECTLY FROM vp_Switch
//Play a sound on Interaction
public virtual void PlaySound()
{

if (AudioSource == null)
return;

if (SwitchSounds.Count == 0)
return;

AudioClip soundToPlay = SwitchSounds[Random.Range(0, SwitchSounds.Count)];

if (soundToPlay == null)
return;

AudioSource.pitch = Random.Range(SwitchPitchRange.x, SwitchPitchRange.y);
AudioSource.PlayOneShot(soundToPlay);

}
}
}

Edy’s Vehicle Physics Inherited Script for Interacting

using UnityEngine;
using System.Collections;
using EVP;
// USING THE BASE VehicleInteractSwitch script and adding EVP controls

namespace RedHawk
{
//Using my namespace.  If you want access, you should include using RedHawk; at the top of the script
public class VehicleInteractSwitchEVP : VehicleInteractSwitch
{

#region Variables-Edy's
//Edy's Vehicle 5.1
VehicleStandardInput m_VehicleStandardInput;
VehicleCameraController m_VehicleCameraController;//NEW
VehicleController m_VehicleController;
#endregion

//OVERRIDE the Base Start
protected override void Start()
{
base.Start();//Run the Base class stuff

//Components ON EVP5.1 Parent Vehicle
m_VehicleStandardInput = Target.GetComponent<VehicleStandardInput>();
if (m_VehicleStandardInput == null)
Debug.LogWarning("Add a VehicleStandardInput to this vehicle!", gameObject);
m_VehicleStandardInput.enabled = false;
m_VehicleController = Target.GetComponent<VehicleController>();
if (m_VehicleController == null)
Debug.LogWarning("Add a VehicleController to this vehicle!", gameObject);
//Components on EVP5 Camera
Camera tmpCam = null;
if (vehicleCamera.GetComponentInChildren<Camera>(true) != null)
tmpCam = vehicleCamera.GetComponentInChildren<Camera>(true);
if (tmpCam == null)
Debug.LogWarning("The Vehicle Camera was not found.  Please add one!", gameObject);
else
vehicleCamera = tmpCam.gameObject;
m_VehicleCameraController = vehicleCamera.transform.parent.gameObject.GetComponentInChildren<VehicleCameraController>(true);
if (m_VehicleCameraController == null)
Debug.LogWarning("Add a VehicleCameraController to the vehicle camera!", vehicleCamera);
m_VehicleCameraController.enabled = false;
m_VehicleController.throttleInput = 0.0f;//Turn off the Throttle
m_VehicleController.brakeInput = 1.0f;//Turn on the Brakes so vehicle does not move
}

//OVERRIDE the Base GetIn
protected override void GetIn()
{
base.GetIn();//Run the Base class stuff
//EVP5 Vehicle Input
m_VehicleStandardInput.enabled = true;
//EVP5 Vehicle Camera Controller
m_VehicleCameraController.enabled = true;
UpdateEVP5CameraSettings();
}

//OVERRIDE the Base GetOut
protected override void GetOut()
{
base.GetOut();//Run the Base class stuff
//EVP5 Vehicle Input
m_VehicleController.throttleInput = 0.0f;//Turn off the Throttle
m_VehicleController.brakeInput = 1.0f;//Turn on the Brakes so vehicle does not move
m_VehicleStandardInput.enabled = false;
//EVP5 Vehicle Camera Controller
m_VehicleCameraController.enabled = false;

}

//Use the VehicleViewConfig from EVP to set up the camera for this vehicle.
void UpdateEVP5CameraSettings()
{
m_VehicleCameraController.target = Target.transform;
m_VehicleCameraController.ResetCamera();
}
}
}

HeaderWP