Hi Hi, Iām a Unity beginner struggling with my first project! :((
I went to Unity Learn and am working on my first project, āTanks: make a battle game for web and mobileā. Iāve added some tanks and made them move! However, when im tring to make the camera following the tank object. I cant find the target button on the control panal. pls help im struggling for about an hour ![]()
here is the script for camera control
using UnityEngine;
public class CameraControl : MonoBehaviour
{
public float m_DampTime = 0.2f;
public float m_ScreenEdgeBuffer = 4f;
public float m_MinSize = 6.5f;
[HideInInspector] public Transform[] m_Targets;
private Camera m_Camera;
private float m_ZoomSpeed;
private Vector3 m_MoveVelocity;
private Vector3 m_DesiredPosition;
private void Awake()
{
m_Camera = GetComponentInChildren<Camera>();
}
private void FixedUpdate()
{
Move();
Zoom();
}
// Public methods to manage targets
public void AddTarget(Transform newTarget)
{
// Create new array with space for additional target
Transform[] newTargets = new Transform[m_Targets != null ? m_Targets.Length + 1 : 1];
// Copy existing targets if they exist
if (m_Targets != null)
{
System.Array.Copy(m_Targets, newTargets, m_Targets.Length);
}
// Add new target at the end
newTargets[newTargets.Length - 1] = newTarget;
m_Targets = newTargets;
}
public void RemoveTarget(Transform targetToRemove)
{
if (m_Targets == null || m_Targets.Length == 0) return;
// Find the index of the target to remove
int index = System.Array.IndexOf(m_Targets, targetToRemove);
if (index < 0) return;
// Create new smaller array
Transform[] newTargets = new Transform[m_Targets.Length - 1];
// Copy all targets except the one to remove
for (int i = 0, j = 0; i < m_Targets.Length; i++)
{
if (i != index)
{
newTargets[j++] = m_Targets[i];
}
}
m_Targets = newTargets;
}
public void ClearAllTargets()
{
m_Targets = new Transform[0];
}
// Original movement and zoom methods remain the same
private void Move()
{
FindAveragePosition();
transform.position = Vector3.SmoothDamp(transform.position, m_DesiredPosition, ref m_MoveVelocity, m_DampTime);
}
private void FindAveragePosition()
{
Vector3 averagePos = new Vector3();
int numTargets = 0;
if (m_Targets != null)
{
for (int i = 0; i < m_Targets.Length; i++)
{
if (m_Targets[i] == null || !m_Targets[i].gameObject.activeSelf)
continue;
averagePos += m_Targets[i].position;
numTargets++;
}
}
if (numTargets > 0)
averagePos /= numTargets;
else
averagePos = transform.position; // Stay in current position if no targets
averagePos.y = transform.position.y;
m_DesiredPosition = averagePos;
}
private void Zoom()
{
float requiredSize = FindRequiredSize();
m_Camera.orthographicSize = Mathf.SmoothDamp(m_Camera.orthographicSize, requiredSize, ref m_ZoomSpeed, m_DampTime);
}
private float FindRequiredSize()
{
if (m_Targets == null || m_Targets.Length == 0)
return m_MinSize;
Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition);
float size = 0f;
for (int i = 0; i < m_Targets.Length; i++)
{
if (m_Targets[i] == null || !m_Targets[i].gameObject.activeSelf)
continue;
Vector3 targetLocalPos = transform.InverseTransformPoint(m_Targets[i].position);
Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;
size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.y));
size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.x) / m_Camera.aspect);
}
size += m_ScreenEdgeBuffer;
size = Mathf.Max(size, m_MinSize);
return size;
}
public void SetStartPositionAndSize()
{
FindAveragePosition();
transform.position = m_DesiredPosition;
m_Camera.orthographicSize = FindRequiredSize();
}
}



