1.类似英雄联盟的相机控制
using UnityEngine;
using System.Collections;
public class CameraCntrl : MonoBehaviour
{
public float camSpeed = 1;
public int GUIsize = 25;
public Transform target;
public bool cameraLocked = false;
void Update()
{
if(Input.GetKeyDown(KeyCode.Y))
{
cameraLocked = !cameraLocked;
}
if(Input.GetKeyDown(KeyCode.Space))
{
ResetCameraPos();
}
if(cameraLocked == false)
{
Rect recdown = new Rect(0, 0, Screen.width, GUIsize);
Rect rectup = new Rect(0, Screen.height - GUIsize, Screen.width, GUIsize);
Rect rectleft = new Rect(0, 0, GUIsize, Screen.height);
Rect rectright = new Rect(Screen.width - GUIsize, 0, GUIsize, Screen.height);
if(recdown.Contains(Input.mousePosition))
{
transform.Translate(0, 0, -camSpeed, Space.World);
}
if (rectup.Contains(Input.mousePosition))
{
transform.Translate(0, 0, camSpeed, Space.World);
}
if (rectleft.Contains(Input.mousePosition))
{
transform.Translate(-camSpeed, 0, 0, Space.World);
}
if (rectright.Contains(Input.mousePosition))
{
transform.Translate(camSpeed, 0, 0, Space.World);
}
}
else if(cameraLocked)
{
ResetCameraPos();
}
}
void ResetCameraPos()
{
Vector3 pos = transform.position;
pos.x = target.transform.position.x;
pos.z = target.transform.position.z - 5;
transform.position = pos;
}
}
2.距离控制
using UnityEngine;
using System.Collections;
public class cameraZoom : MonoBehaviour
{
public float currentFov = 60;
public float sensitivity = 50;
public float damping = 5;
public float minFov = 40;
public float maxFov = 60;
void Start ()
{
currentFov = camera.fieldOfView;
}
// Update is called once per frame
void Update ()
{
currentFov -= Input.GetAxis("Mouse ScrollWheel") * sensitivity;
currentFov = Mathf.Clamp(currentFov, minFov, maxFov);
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, currentFov, Time.deltaTime * damping);
}
}