Skip to content

Instantly share code, notes, and snippets.

@zacyzacy
Created July 4, 2016 17:42
Show Gist options
  • Save zacyzacy/35c78d2c3761a15ecd14dd7f40ccc80f to your computer and use it in GitHub Desktop.
Save zacyzacy/35c78d2c3761a15ecd14dd7f40ccc80f to your computer and use it in GitHub Desktop.
Camera for unity 2.5D fightingng game style
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultiPlayerCamHeuristic : MonoBehaviour
{
public CamPoint[] camPoints;
public CameraClamp[] clamps;
public Vector3 lookPoint, followPoint;
public float camMoveSpeed, camDeadZone, camRotSpeed, zoomSpeed;
//clamps
private Vector3 max, min;
// Use this for initialization
void Start()
{
camPoints = FindObjectsOfType(typeof(CamPoint)) as CamPoint[];
clamps = FindObjectsOfType(typeof(CameraClamp)) as CameraClamp[];
//set clamps
//initial clamp set
setClamp(clamps[0].transform.position);
for (int i = 0; i < clamps.Length; i++)
{
setClamp(clamps[i].transform.position);
}
Debug.Log(min);
Debug.Log(max);
}
// Update is called once per frame
void Update()
{
Debug.DrawLine(transform.position, lookPoint, Color.red);
Debug.DrawLine(transform.position, followPoint, Color.red);
pointCam();
moveCam();
}
void setClamp(Vector3 clamp)
{
if (clamp.x > max.x)
{
max.x = clamp.x;
}
if (clamp.y > max.y)
{
max.y = clamp.y;
}
if (clamp.z > max.z)
{
max.z = clamp.z;
}
if (clamp.x < min.x)
{
min.x = clamp.x;
}
if (clamp.y < min.y)
{
min.y = clamp.y;
}
if (clamp.z < min.z)
{
min.z = clamp.z;
}
}
void pointCam()
{
for (int i = 0; i < camPoints.Length; i++)
{
lookPoint += camPoints[i].transform.position * camPoints[i].weight;
if (lookPoint.y < 4)
{
lookPoint.y = 5;
}
}
lookPoint /= camPoints.Length + 1; //wtf why do i need +1??
if (Vector3.Dot(transform.forward.normalized, (lookPoint - transform.position).normalized) < camDeadZone)
{
transform.forward += Vector3.Lerp(transform.forward, lookPoint - transform.position, camRotSpeed * Time.deltaTime);
}
}
void moveCam()
{
//have a set distance that the camera should be from the middle of the campoints and the clamps stop it from getting to that point
//lerp to correct point
followPoint.z = (camPoints[0].transform.position - lookPoint).magnitude * -zoomSpeed;
followPoint.x = lookPoint.x;
if ((transform.position - lookPoint).magnitude > 1)
{
if (followPoint.x > min.x && followPoint.x < max.x && followPoint.y > min.y && followPoint.y < max.y && followPoint.z > min.z && followPoint.z < max.z)
{
transform.position = Vector3.Lerp(transform.position, followPoint, camMoveSpeed * Time.deltaTime);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment