实现目标:
- 视野随着鼠标指针进行上下左右移动
- 鼠标滚轮控制视野缩放
实现思路:
将摄像机作为其它游戏对象的子物体,通过获取鼠标输入控制其父节点的旋转、移动来实现相机的视野移动和缩放。
层次结构如下:
CameraCrl节点主要是控制视野左右移动,通过旋转其Y轴即可,该节点的position会和跟随目标的position保持一致
Zoom节点主要是负责控制视野缩放,向跟随目标移动即可,这里一般会做一个缩放距离限制,首先我们会将Zoom的Z轴对准目标节点
Pivot主要负责视野上下移动,同样需要作限制
接下来就是通过代码获取输入,控制这几个参数:
PlayerCameraFreeLook类实现跟随主角以及获取输入控制视野(来自官方DEMO),挂载在CameraCrl上
using System;
using UnityEngine;
public class PlayerCameraFreeLook : PlayerCamera
{
// This script is designed to be placed on the root object of a camera rig,
// comprising 3 gameobjects, each parented to the next:
// Camera Rig
// Pivot
// Camera
// This script is designed to be placed on the root object of a camera rig,
// comprising 3 gameobjects, each parented to the next:
protected Transform m_Zoom;
protected Transform m_Pivot; // the point at which the camera pivots around
protected Vector3 m_LastTargetPosition;
[SerializeField] private float m_MoveSpeed = 1f; // How fast the rig will move to keep up with the target's position.
[Range(0f, 10f)] [SerializeField] private float m_TurnSpeed = 1.5f; // How fast the rig will rotate from user input.
[SerializeField] private float m_TurnSmoothing = 0.0f; // How much smoothing to apply to the turn input, to r