wasdswag

FocusRaycast

Apr 14th, 2021
324
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering.PostProcessing;
  5.  
  6.  
  7. public class Focus : MonoBehaviour
  8. {
  9.     private Ray ray;
  10.     private RaycastHit hit;
  11.     private Camera gameCamera;
  12.  
  13.     [SerializeField] private PostProcessVolume postProcessVolume;
  14.  
  15.  
  16.     [SerializeField] private AudioSource music;
  17.     [SerializeField] private Collider ground;
  18.     [SerializeField] private LayerMask focusLayer;
  19.     private bool isFocused, unFocused;
  20.  
  21.  
  22.  
  23.     // Start is called before the first frame update
  24.     void Start()
  25.     {
  26.         gameCamera = GetComponent<Camera>();
  27.     }
  28.  
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.  
  33.         ray = gameCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0f));
  34.  
  35.         if(Physics.Raycast(ray, out hit, 100f, focusLayer))
  36.         {
  37.             Debug.Log("Focused");
  38.             isFocused = true;
  39.  
  40.             if (unFocused)
  41.             {
  42.                 StopAllCoroutines();
  43.                 music.pitch = 1f;
  44.                 postProcessVolume.weight = 0;
  45.                 unFocused = false;
  46.  
  47.             }
  48.  
  49.         }
  50.         else
  51.         {
  52.             isFocused = false;
  53.             if(unFocused == false)
  54.             {
  55.                 StartCoroutine(FallTimeOut());
  56.                 unFocused = true;
  57.             }
  58.  
  59.         }
  60.  
  61.     }
  62.  
  63.  
  64.  
  65.     IEnumerator FallTimeOut()
  66.     {
  67.         float time = 4f;
  68.  
  69.         while (time > 0) {
  70.             music.pitch -= 0.1f * Time.deltaTime;
  71.             postProcessVolume.weight += 0.4f * Time.deltaTime;
  72.             time -= Time.deltaTime;
  73.             yield return null;    
  74.          
  75.             }
  76.  
  77.         ground.enabled = false;
  78.  
  79.     }
  80.  
  81.  
  82. }
  83.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×