Search Unity

Auto-Focus script error

Discussion in 'Scripting' started by Karen_M_2005, Mar 1, 2019.

  1. Karen_M_2005

    Karen_M_2005

    Joined:
    Sep 27, 2018
    Posts:
    12
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.Rendering.PostProcessing;
    5.  
    6.  
    7. [RequireComponent(typeof(Camera))]
    8. [RequireComponent(typeof(DepthOfField))]
    9.  
    10. public class AutoFocus : MonoBehaviour
    11. {
    12.     private GameObject doFFocusTarget;
    13.     private Vector3 lastDoFPoint;
    14.     private DepthOfField dofComponent;
    15.  
    16.     public DoFAFocusQuality focusQuality = AutoFocus.DoFAFocusQuality.NORMAL;
    17.     public LayerMask hitLayer = 1;
    18.     public float maxDistance = 100.0f;
    19.     public bool interpolateFocus = false;
    20.     public float interpolationTime = 0.7f;
    21.  
    22.     public enum DoFAFocusQuality
    23.     {
    24.         NORMAL,
    25.         HIGH
    26.     }
    27.  
    28.     void Start()
    29.     {
    30.         doFFocusTarget = new GameObject("DoFFocusTarget");
    31.         dofComponent = gameObject.GetComponent<DepthOfField>();
    32.         // gives error ad says that 'DepthOfField' does not contain defenition for 'objectFocus' accepting a first argument of type 'DepthOfField' could be found(are you missing a using directive or an assembly refserence?)
    33.         dofComponent.objectFocus = doFFocusTarget.transform;
    34.     }
    35.     void Update()
    36.     {
    37.  
    38.         if (focusQuality == AutoFocus.DoFAFocusQuality.HIGH)
    39.         {
    40.             Focus();
    41.         }
    42.  
    43.     }
    44.  
    45.     void FixedUpdate()
    46.     {
    47.         if (focusQuality == AutoFocus.DoFAFocusQuality.NORMAL)
    48.         {
    49.             Focus();
    50.         }
    51.     }
    52.  
    53.     IEnumerator InterpolateFocus(Vector3 targetPosition)
    54.     {
    55.  
    56.         Vector3 start = this.doFFocusTarget.transform.position;
    57.         Vector3 end = targetPosition;
    58.         float dTime = 0;
    59.  
    60.         Debug.DrawLine(start, end, Color.green);
    61.  
    62.         while (dTime < 1)
    63.         {
    64.             yield return null;
    65.             dTime += Time.deltaTime / this.interpolationTime;
    66.             this.doFFocusTarget.transform.position = Vector3.Lerp(start, end, dTime);
    67.         }
    68.         this.doFFocusTarget.transform.position = end;
    69.     }
    70.  
    71.     void Focus()
    72.     {
    73.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    74.         RaycastHit hit;
    75.         if (Physics.Raycast(ray, out hit, this.maxDistance, this.hitLayer))
    76.         {
    77.             Debug.DrawLine(ray.origin, hit.point);
    78.            
    79.             if (this.lastDoFPoint == hit.point)
    80.             {
    81.                 return;
    82.             }
    83.             else if (this.interpolateFocus)
    84.             {
    85.                 StopCoroutine("InterpolateFocus");
    86.                 StartCoroutine(InterpolateFocus(hit.point));
    87.  
    88.             }
    89.             else
    90.             {
    91.                 this.doFFocusTarget.transform.position = hit.point;
    92.             }
    93.             this.lastDoFPoint = hit.point;
    94.         }
    95.     }
    96.  
    97. }
    98.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    So...what is your error? What do you need help with?
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It is customary to actually say what you need help with when seeking help.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You typically want to include:
    • What result you are trying to achieve
    • A summary of your approach
    • What actually happened instead when you tried it out (or what stopped you from trying it)
    • The relevant portion of your source code