- Home /
Local Multiplayer Camera
Hello everyone i am a newbie in Unity and i am trying to make a simple local multiplayer game, i am facing right now a problem with my perspective camera.I followed a tutorial on youtube and i want to apply that script in my game's scenario.
This is the angle i want to have on my game.The problem is that i use bounds to keep every target within bounds but in this scenario i am confused and not exactly sure if i want to use bounds to keep all targets withing my camera's view.The problem is that players do get out of camera bounds and that make's it unplayable to say the least!Here's the script for the camera
[RequireComponent(typeof(Camera))] public class MultipleCamera_S : MonoBehaviour {
// Use this for initialization
public Transform[] m_Targets;
public Vector3 m_Offset;
private Vector3 m_Velocity;
public float m_SmoothTime = .5f;
public float m_MinZoom = 40f;
public float m_MaxZoom = 10f;
public float m_ZoomLimiter = 50f;
private Camera m_Camera;
public GameObject m_Level;
void Start ()
{
m_Camera = GetComponent<Camera>();
}
// Update is called once per frame
void Update ()
{
}
void CheckIfTargersAreinBounds()
{
for (int i = 0; i < m_Targets.Length; i++)
{
if (m_Targets[i].position.x > m_Level.transform.lossyScale.x && m_Targets[i].position.x < m_Level.transform.lossyScale.x)
{
m_Targets[i] = null;
}
if (m_Targets[i].position.z > m_Level.transform.lossyScale.x && m_Targets[i].position.z < m_Level.transform.lossyScale.z)
{
m_Targets[i] = null;
}
}
}
void LateUpdate()
{
if (m_Targets.Length == 0)
return;
Move();
Zoom();
CheckIfTargersAreinBounds();
}
void Move()
{
Vector3 centerPoint = GetCenterPoint();
Vector3 newPosition = centerPoint + m_Offset;
transform.position = Vector3.SmoothDamp(transform.position, newPosition, ref m_Velocity, m_SmoothTime);
}
void Zoom()
{
float newZoom = Mathf.Lerp(m_MaxZoom, m_MinZoom, GetGreatestDistance()/ m_ZoomLimiter);
m_Camera.fieldOfView = Mathf.Lerp(m_Camera.fieldOfView,newZoom,Time.deltaTime);
}
float GetGreatestDistance()
{
var bounds = new Bounds(m_Targets[0].position, Vector3.zero);
for (int i = 0; i < m_Targets.Length; i++)
{
bounds.Encapsulate(m_Targets[i].position);
}
return bounds.size.x;
}
Vector3 GetCenterPoint()
{
if (m_Targets.Length == 1)
{
return m_Targets[0].position;
}
var bounds = new Bounds(m_Targets[0].position,Vector3.zero);
//camera fix
for (int i = 0; i < m_Targets.Length; i++)
{
bounds.Encapsulate(m_Targets[i].position);
}
return bounds.center;
}
}
I understand i am adding only the x component of the bounds which doesnt make sense but adding the z as well wont help in this case!Any ideas or help would be welcome!Thanks in advance!
Your answer
Follow this Question
Related Questions
New Input Manager Split Screen and Cinemachine 0 Answers
Local Mulitplayer 2d Camera 0 Answers
TopDown local multiplayer camera edges collision? 1 Answer
Mathf.SmoothDamp happens instantly 1 Answer