一、开发环境
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 Vector30, 0, Mathf.Clampm_CameraUpAndDown.transform.localEulerAngles.z, 35f, 80f));}/// <summary>/// 设置摄像机旋转/// </summary>/// <param name="type">0=左 1=右</param>public void SetCameraRotateint type){transform.Rotate0, 80 * Time.deltaTime * type == 0 ? -1 : 1), 0);}/// <summary>/// 设置摄像机上下 0=上 1=下/// </summary>/// <param name="type"></param>public void SetCameraUpAndDownint type){m_CameraUpAndDown.transform.Rotate0, 0, 60 * Time.deltaTime * type == 1 ? -1 : 1));m_CameraUpAndDown.transform.localEulerAngles = new Vector30, 0, Mathf.Clampm_CameraUpAndDown.transform.localEulerAngles.z, 5f, 80f));}/// <summary>/// 设置摄像机 缩放/// </summary>/// <param name="type">0=拉近 1=拉远</param>public void SetCameraZoomint type){m_CameraContainer.TranslateVector3.forward * 10 * Time.deltaTime * type == 1 ? -1 : 1)));m_CameraContainer.localPosition = new Vector30, 0, Mathf.Clampm_CameraContainer.localPosition.z, -10f, 10f));}/// <summary>/// 实时看着主角/// </summary>/// <param name="pos"></param>public void AutoLookAtVector3 pos){m_CameraZoomContainer.LookAtpos);}//void OnDrawGizmos)//{// Gizmos.color = Color.red;// Gizmos.DrawWireSpheretransform.position, 15f);// Gizmos.color = Color.blue;// Gizmos.DrawWireSpheretransform.position, 14f);// Gizmos.color = Color.green;// Gizmos.DrawWireSpheretransform.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 updatevoid Start){if CameraCtrl.Instance != null){CameraCtrl.Instance.Init);}}// Update is called once per framevoid Update){// 摄像机自动跟随CameraAutoFollow);}#region CameraAutoFollow 摄像机自动跟随/// <summary>/// 摄像机自动跟随/// </summary>private void CameraAutoFollow){if CameraCtrl.Instance == null) return;CameraCtrl.Instance.transform.position = gameObject.transform.position;CameraCtrl.Instance.AutoLookAtgameObject.transform.position + new Vector30, 1.2f, 0));if Input.GetKeyKeyCode.A)){CameraCtrl.Instance.SetCameraRotate0);}else if Input.GetKeyKeyCode.D)){CameraCtrl.Instance.SetCameraRotate1);}else if Input.GetKeyKeyCode.W)){CameraCtrl.Instance.SetCameraUpAndDown0);}else if Input.GetKeyKeyCode.S)){CameraCtrl.Instance.SetCameraUpAndDown1);}else if Input.GetKeyKeyCode.I)){CameraCtrl.Instance.SetCameraZoom0);}else if Input.GetKeyKeyCode.L)){CameraCtrl.Instance.SetCameraZoom1);}}#endregion
}