Realistic FPS and Edy’s

This is an integration of Edy’s Vehicle Physics (AWESOME!) and Realistic First Person Shooter.

For this integration, I placed the below script on the L200-Blue vehicle MeshCollider child called Top. Assign the variables, but you can ignore the Exit Position as it will automatically create one.
NOTE: Change Vehicle Camera tag to Untagged, Assign Vehicle Camera, Set Usable tag to wherever you place the below script.

https://youtu.be/Qlbm7OSZTGQ

Edy’s Vehicle Physics Script for Interacting

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EVP;
///NOTES
/// Set this object tag to Usable, ensure Vehicle Camera is not tagged as MainCamera
/// 
namespace RedHawk
{
	public class VehicleInteractRFPS : MonoBehaviour {
		
		#region Variables - Edy's
		VehicleStandardInput m_VehicleStandardInput;
		VehicleCameraController m_VehicleCameraController;
		VehicleController m_VehicleController;
		Transform m_Target;
		#endregion
		[Tooltip("Ensure your Vehicle Camera is untagged.  If it is tagged as MainCamera, RFPS will not work properly.")]
		public GameObject vehicleCamera = null;
		[Tooltip("Vehicle Child Game Object which represents the position the player will exit at.  If omitted, one will be automatically created.")]
		public GameObject exitPosition = null;
		bool inVehicle=false;
		#region Variables - RFPS
		private FPSPlayer FPSPlayerComponent;
		GameObject m_Body = null;
		bool m_BodyVisible = false;
		GameObject m_MainCamera = null;
		GameObject m_WeaponCamera = null;
		GameObject m_WeaponObj = null;
		#endregion
		void Awake(){
			//Camera Checks and initial setup due to RFPS
			Camera tmpCam = null;
			if (vehicleCamera.GetComponentInChildren<Camera> (true) != null)
				tmpCam = vehicleCamera.GetComponentInChildren<Camera> (true);
			if (tmpCam == null)
				Debug.Log ("The Vehicle Camera was not found.  Please add one!", gameObject);
			if (tmpCam.tag == "MainCamera") {
				Debug.Log ("The Vehicle Camera is set to MainCamera.  This needs to be changed for RFPS", vehicleCamera);
				vehicleCamera.SetActive (false);
				this.enabled = false;
			}
			vehicleCamera.GetComponentInChildren<Camera> (true).enabled = false;
			vehicleCamera.GetComponentInChildren<AudioListener> (true).enabled = false;
			if (this.tag != "Usable") {
				Debug.Log ("The tag of this object should be set to Usable to work with RFPS", this);
				this.enabled = false;
			}
			m_MainCamera = Camera.main.gameObject;
			m_WeaponCamera = m_MainCamera.transform.GetComponentInParent<ReconfigurePrefab> ().WeaponCamera;
			m_WeaponObj = m_MainCamera.transform.GetComponentInParent<ReconfigurePrefab> ().WeaponObj;
		}
		public virtual void Start(){
			
			//Components on EVP Parent Vehicle
			m_VehicleStandardInput = this.GetComponentInParent<VehicleStandardInput>();
			m_VehicleStandardInput.enabled = false;
			m_VehicleController = this.GetComponentInParent<VehicleController> ();
			m_Target = m_VehicleController.transform;
			//Components on EVP Camera
			m_VehicleCameraController = vehicleCamera.GetComponentInParent<VehicleCameraController> ();
			if (m_VehicleCameraController == null)
				Debug.Log ("Add a VehicleCameraController to the vehicle camera!", vehicleCamera);
			m_VehicleCameraController.enabled = false;
			m_VehicleController.throttleInput = 0.0f;
			m_VehicleController.brakeInput = 1.0f;
			//Exit Position
			if(exitPosition == null){
				exitPosition = new GameObject("ExitPosition");
				exitPosition.transform.SetParent(m_Target);
				exitPosition.transform.localPosition = new Vector3(-1f, 0f, 0f);
				exitPosition.transform.localRotation = Quaternion.identity;
			}
			//RFPS
			FPSPlayerComponent = m_MainCamera.GetComponent<CameraControl> ().playerObj.GetComponent<FPSPlayer> ();
			if (FPSPlayerComponent.gameObject.GetComponent<FPSRigidBodyWalker> ().VisibleBody != null)
				m_Body = FPSPlayerComponent.gameObject.GetComponent<FPSRigidBodyWalker> ().VisibleBody;
			if (m_Body != null)
				m_BodyVisible = FPSPlayerComponent.gameObject.GetComponent<FPSRigidBodyWalker> ().displayVisibleBody;
				
		}
		void FixedUpdate()
		{
			if (inVehicle) {
				if (Input.GetButtonDown ("Use"))
					ExitVehicle ();
			}
		}
		//RFPS Called Action for Usable Objects - see FPSPlayer
		public virtual void ActivateObject()
		{
			//Debug.Log ("We've Been Activated");
			EnterVehicle ();
			inVehicle = true;
		}

		//Enter Vehicle
		public virtual void EnterVehicle()
		{
			//Debug.Log ("We Entered Vehicle!");
			//RFPS Player
			FPSPlayerComponent.gameObject.SetActive(false);
			FPSPlayerComponent.transform.SetParent (m_Target);
			FPSPlayerComponent.transform.position = m_Target.position;
			if (m_BodyVisible) { //If DisplayVisibleBody is true, then handle the body
				m_Body.gameObject.SetActive (false);
				m_Body.transform.SetParent (m_Target);
				m_Body.transform.position = m_Target.position;
			}
			//RFPS Camera
			m_MainCamera.GetComponent<AudioListener> ().enabled = false;
			m_MainCamera.GetComponent<Camera>().enabled = false;
			m_WeaponCamera.GetComponent<Camera> ().enabled = false;
			m_WeaponObj.SetActive (false);
			//Vehicle Camera
			vehicleCamera.GetComponentInChildren<Camera> (true).enabled = true;
			vehicleCamera.GetComponentInChildren<AudioListener> (true).enabled = true;
			//EDYS
			m_VehicleStandardInput.enabled = true;
			m_VehicleCameraController.enabled = true;
			UpdateEVPCameraSettings ();
		}

		//Exit Vehicle
		public virtual void ExitVehicle()
		{
			//Debug.Log ("We Exited Vehicle!");
			inVehicle = false;
			//RFPS Player
			FPSPlayerComponent.transform.parent = null;
			FPSPlayerComponent.transform.position = exitPosition.transform.position;
			FPSPlayerComponent.transform.rotation = exitPosition.transform.rotation;
			FPSPlayerComponent.gameObject.SetActive(true);
			if (m_BodyVisible) { //If DisplayVisibleBody is true, then handle the body
				m_Body.transform.parent = null;
				m_Body.transform.position = exitPosition.transform.position;
				m_Body.transform.rotation = exitPosition.transform.rotation;
				m_Body.gameObject.SetActive (true);
			}
			//RFPS Camera
			m_MainCamera.GetComponent<Camera>().enabled = true;
			m_MainCamera.GetComponent<AudioListener> ().enabled = true;
			m_WeaponObj.SetActive (true);
			m_WeaponCamera.GetComponent<Camera> ().enabled = true;
			//Vehicle Camera
			vehicleCamera.GetComponentInChildren<Camera> (true).enabled = false;
			vehicleCamera.GetComponentInChildren<AudioListener> (true).enabled = false;
			//EDYS
			m_VehicleController.throttleInput = 0.0f;
			m_VehicleController.brakeInput = 1.0f;
			m_VehicleStandardInput.enabled = false;
			m_VehicleCameraController.enabled = false;
		}
		//User EVP Functionality
		void UpdateEVPCameraSettings()
		{
			m_VehicleCameraController.target = m_Target;
			m_VehicleCameraController.ResetCamera();
		}
	}
}

HeaderWP

Advertisements