so in my game i have a local co-op and i want a single camera to follow the players. this camera should be at a specified distance from the players but zoom in or zoom out, smoothly based on the distance between the two players like most old coop games. i have attempted to make it but it does not really work. can anyone suggest how to fix what i have or a whole new way of doing it. any help appreciated.
public class CameraFollow : MonoBehaviour
{
public Vector3 distance;
public Transform target1;
public Transform target2;
public float CamZPosition;
public float TargetZmiddle;
public float camDistance;
public float trial;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
distance = target1.position - target2.position;
TargetZmiddle = ((target1.position.z * target2.position.z) /2);
if(distance.x < 0)
distance.x = distance.x * -1;
if(distance.z < 0)
distance.z = distance.z * -1;
trial = camDistance + distance.x;
if(distance.z > 20)
{
//camDistance = trial;
}
if(distance.x > 20)
{
camDistance = trial;
}
if(distance.x < 19 || distance.z < 19)
{
camDistance = 10;
}
//Debug.Log (TargetZmiddle);
transform.position = new Vector3(transform.position.x, transform.position.y, (TargetZmiddle - camDistance));
}
}
for anyone that wants to know i have completed this function
the final code is below
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CameraFollow : MonoBehaviour
{
public static CameraFollow cFollow;
public float dampTime = 0.15f;
private Vector3 velocity = Vector3.zero;
public Transform target;
public float MidX;
public float MidY;
public float MidZ;
public Transform target1;
public Transform target2;
public Vector3 Midpoint;
public Vector3 distance;
public float camDistance;
public float CamOffset;
public float bounds;
void Awake ()
{
//checking if there's already a game manager
if(cFollow == null)
{
DontDestroyOnLoad (gameObject);
cFollow = this;
}
else if(cFollow != this)
{
Destroy(gameObject);
}
}
// Use this for initialization
void Start ()
{
camDistance = 10.0f;
bounds = 12.0f;
}
// Update is called once per frame
void Update ()
{
distance = target1.position - target2.position;
if(camDistance >= 19.0f)
camDistance = 19.0f;
if (camDistance <= 10.0f)
camDistance = 10.0f;
if(distance.x < 0)
distance.x = distance.x * -1;
if(distance.z < 0)
distance.z = distance.z * -1;
if(target1.position.x < (transform.position.x -bounds))
{
Vector3 pos = target1.position;
pos.x = transform.position.x -bounds;
target1.position = pos;
}
if(target2.position.x < (transform.position.x -bounds))
{
Vector3 pos = target2.position;
pos.x = transform.position.x -bounds;
target2.position = pos;
}
if(target1.position.x > (transform.position.x +bounds))
{
Vector3 pos = target1.position;
pos.x = transform.position.x +bounds;
target1.position = pos;
}
if(target2.position.x > (transform.position.x +bounds))
{
Vector3 pos = target2.position;
pos.x = transform.position.x +bounds;
target2.position = pos;
}
if(distance.x > 15.0f)
{
CamOffset = distance.x * 0.3f;
if(CamOffset >=8.5f)
CamOffset = 8.5f;
}else if(distance.x < 14.0f)
{
CamOffset = distance.x * 0.3f;
}else if( distance.z < 14.0f)
{
CamOffset = distance.x * 0.3f;
}
MidX = (target2.position.x + target1.position.x) /2;
MidY = (target2.position.y + target1.position.y) /2;
MidZ = (target2.position.z + target1.position.z) /2;
Midpoint = new Vector3 (MidX, MidY, MidZ);
if (target1)
{
Vector3 point = camera.WorldToViewportPoint(Midpoint);
Vector3 delta = Midpoint - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, camDistance + CamOffset)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
}
hellp hi bro i want use this script but is not working