Guest User

FOVCont

a guest
Jan 19th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. /////////////////////////////////////
  2. //
  3. //
  4. //      Invector - FOV Auto Update v0.1
  5. //      
  6. //      Created By: Tsuyoi Raion
  7. //      Contact: dizzymediainc@gmail.com
  8. //
  9. //
  10. //      Description:
  11. //      Auto updates the FOV for the camera based on distance from camera to the player utilizing the values provided in the inspector for this script (i.e maxRange, minFOV, maxFOV, etc.)
  12. //
  13. //      The script will auto grab the initialFOV from the camera state FOV value and save it for resetting the FOV via runtime as well as when the script is either destroyed or the game stops playing (i.e OnDestroy)
  14. //
  15. //      Instructions:
  16. //      To use this asset simply place it on an object in the scene or on the camera object and place in the camera object transform as well as the player transform into the inspector values.
  17. //      Provided are default values that'll auto set once you place the script on an object, you can of course adjust these values in the inspector to meet your needs.
  18. //
  19. //      Each value (i.e stateName, etc.) has instructions/details for it's usage, these will display when the mouse hovers over the value in the inspector.
  20. //      
  21. //
  22. //
  23. //
  24. /////////////////////////////////////
  25.  
  26.  
  27.  
  28.  
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using UnityEngine;
  32. using Invector;
  33.  
  34. public class FOVCont : MonoBehaviour {
  35.    
  36.     [Header("References")]
  37.    
  38.     [Tooltip("Camera State Reference i.e top down, mansion, etc.")]
  39.     public vThirdPersonCameraListData CameraStateList;
  40.    
  41.     [Header("State")]
  42.    
  43.     [Tooltip("State Name to check in camera state i.e default, mansion, etc.")]
  44.     public string stateName = "Mansion";
  45.    
  46.     [Header("Transforms")]
  47.    
  48.     [Tooltip("Camera Transform -Parent-")]
  49.     public Transform camTrans;
  50.    
  51.     [Tooltip("Player Transform -Parent-")]
  52.     public Transform playTrans;
  53.  
  54.     [Header("FOV Settings")]
  55.    
  56.     [Tooltip("Max range for distance to check for player from camera, updates FOV")]
  57.     public float maxRange = 6;
  58.    
  59.     [Tooltip("Minimum FOV to adjust to, i.e 30, 40, 50, etc.")]
  60.     public float fovMin = 50;
  61.    
  62.     [Tooltip("Minimum FOV to adjust to, i.e 50, 60, 70, etc.")]
  63.     public float fovMax = 60;
  64.    
  65.     [Tooltip("Speed in which FOV updates")]
  66.     public float fovSpeed = 0.02f;
  67.    
  68.     [Tooltip("Should FOV auto update?")]
  69.     public bool fovUpdate = true;
  70.    
  71.     [Header("-AUTO-")]
  72.    
  73.     [Tooltip("Initial FOV from camera state fov value")]
  74.     public float initialFOV = 0;
  75.    
  76.     [Tooltip("Current FOV value")]
  77.     public float curFOV = 0;
  78.    
  79.     [Tooltip("Lock bool, not actually used yet")]
  80.     public bool locked = false;
  81.  
  82.     // Use this for initialization
  83.     void Start () {
  84.        
  85.         vThirdPersonCameraState state = CameraStateList != null ? CameraStateList.tpCameraStates.Find(delegate (vThirdPersonCameraState obj) { return obj.Name.Equals(stateName); }) : new vThirdPersonCameraState("Default");
  86.  
  87.         initialFOV = state.fov;
  88.     }
  89.    
  90.     void ResetFOV(){
  91.        
  92.             vThirdPersonCameraState state = CameraStateList != null ? CameraStateList.tpCameraStates.Find(delegate (vThirdPersonCameraState obj) { return obj.Name.Equals(stateName); }) : new vThirdPersonCameraState("Default");
  93.        
  94.             state.fov = initialFOV;
  95.     }
  96.    
  97.     // Update is called once per frame
  98.     void Update () {
  99.        
  100.         if(fovUpdate){
  101.            
  102.             vThirdPersonCameraState state = CameraStateList != null ? CameraStateList.tpCameraStates.Find(delegate (vThirdPersonCameraState obj) { return obj.Name.Equals(stateName); }) : new vThirdPersonCameraState("Default");
  103.        
  104.             //check if distance between object, player plosition and max range assigned
  105.             if(Vector3.Distance(camTrans.position, playTrans.position) > maxRange) {    
  106.            
  107.                 if(state.fov <= fovMin){
  108.                
  109.                     state.fov = fovMin;
  110.                
  111.                 } else {
  112.                
  113.                     state.fov -= fovSpeed;
  114.                 }
  115.                
  116.             } else if(Vector3.Distance(camTrans.position, playTrans.position) < maxRange){
  117.            
  118.                 state.fov += fovSpeed;
  119.            
  120.                 if(state.fov >= initialFOV){
  121.                
  122.                     ResetFOV();
  123.                 }
  124.             }
  125.        
  126.             curFOV = state.fov;
  127.         }
  128.        
  129.     }
  130.    
  131.    
  132.     void OnDestroy(){
  133.        
  134.             vThirdPersonCameraState state = CameraStateList != null ? CameraStateList.tpCameraStates.Find(delegate (vThirdPersonCameraState obj) { return obj.Name.Equals(stateName); }) : new vThirdPersonCameraState("Default");
  135.        
  136.             state.fov = initialFOV;
  137.     }
  138. }
Add Comment
Please, Sign In to add comment