我正在尝试制作一个脚本,该脚本将根据相机与"节点"的接近程度动态改变近剪辑平面的距离。我把它放下。唯一的问题是,我不确定如何在c#脚本中引用相机组件上的近剪辑平面字段,以便在运行时更改它。摄像机组件的图片供参考。
还有,下面是目前为止的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NodeManager : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
List<Transform> nodeArray = new List<Transform>;
var parentNode = transform;
Transform[] children = GetComponentsOnChildren<Transform>();
foreach (Transform child in children)
{
nodeArray.Add(child);
}
List<float> distanceMagnitudes = new List<float>;
}
// Update is called once per frame
void Update()
{
for(var i = 0; i < parentNode.childCount; i++)
{
Vector3 offset = nodeArray[i].position - this.position;
distanceMagnitudes[i] = offset.sqrMagnitude;
}
float distanceToClosest = sqrt(distanceMagnitudes.min());
if(distanceToClosest < 10)
{
parentNode.Find("Camera Offset").Find("Main Camera").
}
}
}
首先,尽量不要使用Find("Main Camera")
在您的场景中获取相机,而是在Start()
方法中定义相机并像这样设置它的值,或者将其公开并在检查器中设置
Camera mainCamera;
private void Start()
{
// find by type if you will only have one camera always
mainCamera = FindObjectOfType<Camera>();
// find by tag if you might have several cameras
mainCamera = GameObject.FindWithTag("Your Camera Tag").GetComponent<Camera>();
}
你可以得到附近的剪辑,无论…
mainCamera.nearClipPlane = 1.0f; //Your Value