我正在做“坦克!” Unity教程和指南告诉我删除tank对象,因为它是一个Prefab,将使用派生点代替。我跟踪了视频,但它告诉我尚未分配CameraControl的m_Targets,因为它隐藏在检查器中,并且不能根据视频进行修改。我已经重新检查了所有这些,这是我的不得已。我尝试修改所有其他脚本,但是无论我做什么,它们都无法正常工作。我尝试使用已完成版本的水箱以及已完成的所有操作,但均无法使用,甚至包括Camera控制脚本本身。如何获得将目标设置为2并将坦克的预制件用于相机目标的方法?
CameraControl.cs:
using UnityEngine;
namespace Complete
{
public class CameraControl : MonoBehaviour
{
public float m_DampTime = 0.2f; // Approximate time for the camera to refocus.
public float m_ScreenEdgeBuffer = 4f; // Space between the top/bottom most target and the screen edge.
public float m_MinSize = 6.5f;
/*[HideInInspector] */ public Transform[] m_Targets; // All the targets the camera needs to encompass.
private Camera m_Camera; // Used for referencing the camera.
private float m_ZoomSpeed; // Reference speed for the smooth damping of the orthographic size.
private Vector3 m_MoveVelocity; // Reference velocity for the smooth damping of the position.
private Vector3 m_DesiredPosition; // The position the camera is moving towards.
private void Awake ()
{
m_Camera = GetComponentInChildren<Camera> ();
}
private void FixedUpdate ()
{
// Move the camera towards a desired position.
Move ();
// Change the size of the camera based.
Zoom ();
}
private void Move ()
{
// Find the average position of the targets.
FindAveragePosition ();
// Smoothly transition to that position.
transform.position = Vector3.SmoothDamp(transform.position, m_DesiredPosition, ref m_MoveVelocity, m_DampTime);
}
private void FindAveragePosition ()
{
Vector3 averagePos = new Vector3 ();
int numTargets = 0;
// Go through all the targets and add their positions together.
for (int i = 0; i < m_Targets.Length; i++)
{
// If the target isn't active, go on to the next one.
if (!m_Targets[i].gameObject.activeSelf)
continue;
// Add to the average and increment the number of targets in the average.
averagePos += m_Targets[i].position;
numTargets++;
}
// If there are targets divide the sum of the positions by the number of them to find the average.
if (numTargets > 0)
averagePos /= numTargets;
// Keep the same y value.
averagePos.y = transform.position.y;
// The desired position is the average position;
m_DesiredPosition = averagePos;
}
private void Zoom ()
{
// Find the required size based on the desired position and smoothly transition to that size.
float requiredSize = FindRequiredSize();
m_Camera.orthographicSize = Mathf.SmoothDamp (m_Camera.orthographicSize, requiredSize, ref m_ZoomSpeed, m_DampTime);
}
private float FindRequiredSize ()
{
// Find the position the camera rig is moving towards in its local space.
Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition);
// Start the camera's size calculation at zero.
float size = 0f;
// Go through all the targets...
for (int i = 0; i < m_Targets.Length; i++)
{
// ... and if they aren't active continue on to the next target.
if (!m_Targets[i].gameObject.activeSelf)
continue;
// Otherwise, find the position of the target in the camera's local space.
Vector3 targetLocalPos = transform.InverseTransformPoint(m_Targets[i].position);
// Find the position of the target from the desired position of the camera's local space.
Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;
// Choose the largest out of the current size and the distance of the tank 'up' or 'down' from the camera.
size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.y));
// Choose the largest out of the current size and the calculated size based on the tank being to the left or right of the camera.
size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.x) / m_Camera.aspect);
}
// Add the edge buffer to the size.
size += m_ScreenEdgeBuffer;
// Make sure the camera's size isn't below the minimum.
size = Mathf.Max (size, m_MinSize);
return size;
}
public void SetStartPositionAndSize ()
{
// Find the desired position.
FindAveragePosition ();
// Set the camera's position to the desired position without damping.
transform.position = m_DesiredPosition;
// Find and set the required size of the camera.
m_Camera.orthographicSize = FindRequiredSize ();
}
}
}
[不幸的是,一些“坦克!”教程步骤依赖于教程的后续部分。特别是步骤6:游戏管理员。
由于m_Targets被公开为公共字段,因此可以从其他脚本中进行设置。这正是GameManager脚本在调用时所做的:
private void SetCameraTargets()
{
// Create a collection of transforms the same size as the number of tanks.
Transform[] targets = new Transform[m_Tanks.Length];
// For each of these transforms...
for (int i = 0; i < targets.Length; i++)
{
// ... set it to the appropriate tank transform.
targets[i] = m_Tanks[i].m_Instance.transform;
}
// These are the targets the camera should follow.
m_CameraControl.m_Targets = targets;
}
换句话说,不用担心坦克还没有被设置为摄像机目标。这是在本教程的第6步中配置的。