Code (CSharp): using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.Rendering.PostProcessing; [RequireComponent(typeof(Camera))] [RequireComponent(typeof(DepthOfField))] public class AutoFocus : MonoBehaviour { private GameObject doFFocusTarget; private Vector3 lastDoFPoint; private DepthOfField dofComponent; public DoFAFocusQuality focusQuality = AutoFocus.DoFAFocusQuality.NORMAL; public LayerMask hitLayer = 1; public float maxDistance = 100.0f; public bool interpolateFocus = false; public float interpolationTime = 0.7f; public enum DoFAFocusQuality { NORMAL, HIGH } void Start() { doFFocusTarget = new GameObject("DoFFocusTarget"); dofComponent = gameObject.GetComponent<DepthOfField>(); // 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?) dofComponent.objectFocus = doFFocusTarget.transform; } void Update() { if (focusQuality == AutoFocus.DoFAFocusQuality.HIGH) { Focus(); } } void FixedUpdate() { if (focusQuality == AutoFocus.DoFAFocusQuality.NORMAL) { Focus(); } } IEnumerator InterpolateFocus(Vector3 targetPosition) { Vector3 start = this.doFFocusTarget.transform.position; Vector3 end = targetPosition; float dTime = 0; Debug.DrawLine(start, end, Color.green); while (dTime < 1) { yield return null; dTime += Time.deltaTime / this.interpolationTime; this.doFFocusTarget.transform.position = Vector3.Lerp(start, end, dTime); } this.doFFocusTarget.transform.position = end; } void Focus() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, this.maxDistance, this.hitLayer)) { Debug.DrawLine(ray.origin, hit.point); if (this.lastDoFPoint == hit.point) { return; } else if (this.interpolateFocus) { StopCoroutine("InterpolateFocus"); StartCoroutine(InterpolateFocus(hit.point)); } else { this.doFFocusTarget.transform.position = hit.point; } this.lastDoFPoint = hit.point; } } }
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