一、开发环境
Unity版本: 2020.1.0f1
二、建立摄像机结构
1、结构如下图
2、将CameraZoomContainer的Position X坐标设置为12,如图
3、将CameraUpAndDown的Rotation Z坐标设置为30,如图:
三、实现摄像机的跟随、旋转、缩放
1、编写相机控制器脚本CameraCtrl挂载到CameraFollowAndRotate上,脚本内容如下:
using UnityEngine;
using System.Collections;
/// <summary>
///
/// </summary>
public class CameraCtrl : MonoBehaviour
{
public static CameraCtrl Instance;
/// <summary>
/// 控制摄像机上下
/// </summary>
[SerializeField]
private Transform m_CameraUpAndDown;
/// <summary>
/// 摄像机缩放父物体
/// </summary>
[SerializeField]
private Transform m_CameraZoomContainer;
/// <summary>
/// 摄像机容器
/// </summary>
[SerializeField]
private Transform m_CameraContainer;
void Awake()
{
Instance = this;
}
void Start()
{
}
/// <summary>
/// 初始化
/// </summary>
public void Init()
{
m_CameraUpAndDown.transform.localEulerAngles = new Vector3(0, 0, Mathf.Clamp(m_CameraUpAndDown.transform.localEulerAngles.z, 35f, 80f));
}
/// <summary>
/// 设置摄像机旋转
/// </summary>
/// <param name="type">0=左 1=右</param>
public void SetCameraRotate(int type)
{
transform.Rotate(0, 80 * Time.deltaTime * (type == 0 ? -1 : 1), 0);
}
/// <summary>
/// 设置摄像机上下 0=上 1=下
/// </summary>
/// <param name="type"></param>
public void SetCameraUpAndDown(int type)
{
m_CameraUpAndDown.transform.Rotate(0, 0, 60 * Time.deltaTime * (type == 1 ? -1 : 1));
m_CameraUpAndDown.transform.localEulerAngles = new Vector3(0, 0, Mathf.Clamp(m_CameraUpAndDown.transform.localEulerAngles.z, 5f, 80f));
}
/// <summary>
/// 设置摄像机 缩放
/// </summary>
/// <param name="type">0=拉近 1=拉远</param>
public void SetCameraZoom(int type)
{
m_CameraContainer.Translate(Vector3.forward * 10 * Time.deltaTime * ((type == 1 ? -1 : 1)));
m_CameraContainer.localPosition = new Vector3(0, 0, Mathf.Clamp(m_CameraContainer.localPosition.z, -10f, 10f));
}
/// <summary>
/// 实时看着主角
/// </summary>
/// <param name="pos"></param>
public void AutoLookAt(Vector3 pos)
{
m_CameraZoomContainer.LookAt(pos);
}
//void OnDrawGizmos()
//{
// Gizmos.color = Color.red;
// Gizmos.DrawWireSphere(transform.position, 15f);
// Gizmos.color = Color.blue;
// Gizmos.DrawWireSphere(transform.position, 14f);
// Gizmos.color = Color.green;
// Gizmos.DrawWireSphere(transform.position, 12f);
//}
}
2、设置变量,如图:
3、编写角色控制器脚本RoleCtrl,将角色脚本挂载到角色上(本脚本未实现角色移动,角色移动请参考其他资料)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoleCtrl : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
if (CameraCtrl.Instance != null)
{
CameraCtrl.Instance.Init();
}
}
// Update is called once per frame
void Update()
{
// 摄像机自动跟随
CameraAutoFollow();
}
#region CameraAutoFollow 摄像机自动跟随
/// <summary>
/// 摄像机自动跟随
/// </summary>
private void CameraAutoFollow()
{
if (CameraCtrl.Instance == null) return;
CameraCtrl.Instance.transform.position = gameObject.transform.position;
CameraCtrl.Instance.AutoLookAt(gameObject.transform.position + new Vector3(0, 1.2f, 0));
if (Input.GetKey(KeyCode.A))
{
CameraCtrl.Instance.SetCameraRotate(0);
}
else if (Input.GetKey(KeyCode.D))
{
CameraCtrl.Instance.SetCameraRotate(1);
}
else if (Input.GetKey(KeyCode.W))
{
CameraCtrl.Instance.SetCameraUpAndDown(0);
}
else if (Input.GetKey(KeyCode.S))
{
CameraCtrl.Instance.SetCameraUpAndDown(1);
}
else if (Input.GetKey(KeyCode.I))
{
CameraCtrl.Instance.SetCameraZoom(0);
}
else if (Input.GetKey(KeyCode.L))
{
CameraCtrl.Instance.SetCameraZoom(1);
}
}
#endregion
}
4、测试
运行游戏,按键A与D进行旋转,按键W与S进行调整相机角度,按键I与L进行拉进拉远
5、手势操作-未完待续
|