这一次我们在上一篇自由视角的基础上加上锁定视角的功能,没有看过上一篇的点下方链接。
Unity第三人称的实现思路(一)
锁定视角的动画分为两种情况,一种是不带方向的一种是带方向的:
Cinemachine组件很多参数都是公开的,只需要引用Cinemachine命名空间然后获取组件就能修改其中的参数。锁定视角需要更改的参数其实只有两三处,可以在锁定视角时用代码修改,这里为了减少代码量没有采用这种方法,毕竟现在需要修改的只有两三处,但以后就不一定了。在上一篇中提到的Priority(优先级)参数就起到作用了,主摄像机会渲染处于激活状态的更高优先级的虚拟相机,我们只需要复制一份自由视角的相机然后修改其中的参数,再用代码动态激活和关闭这个相机即可。
Axis Control中两个轴的speed设置成0是为了让鼠标两个轴的输入变为0,相当于禁止移动视角。
Binding Mode原本是World Space,如果不移动视角的话位置就不会改变,锁定视角时会出现相机在目标与角色中间,看不到角色的问题。设置成Lock To Target后相机位置会锁定在角色身后,只要让角色时刻朝向目标,摄像机就永远能看见角色并对准目标。
//如果按下锁定键
if(Input.GetKeyDown(keys.Lock))
{
//锁定状态改变
state.isLocking = !state.isLocking;
//锁定为真打开锁定视角摄像机,锁定为假时关闭
lockCamera.SetActive(state.isLocking);
//设置动画状态机锁定状态的参数
animController.SetBool("LockState", state.isLocking);
}
//锁定状态时每一帧调用
void LockLook()
{
if (!state.isLocking)
return;
//不移动时不旋转角色
if (moveDir.sqrMagnitude < 0.1f)
return;
//相机朝向目标,旋转值赋给角色后角色也朝向目标
Vector3 cameraEuler = Camera.main.transform.eulerAngles;
transform.rotation = Quaternion.Euler(0, cameraEuler.y, 0);
//非战斗状态(空手)
if(!state.isEquiped)
{
//键盘输入的方向
Vector3 dir = moveDir.normalized;
float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;
Quaternion targetDir = Quaternion.Euler(0, angle, 0);
//模型朝向键盘输入方向
model.localRotation = Quaternion.Lerp(model.localRotation, targetDir, 12 * Time.deltaTime);
}
//战斗状态(拔剑)
else
{
//模型本地旋转设置为0
model.localRotation = Quaternion.Lerp(model.localRotation, Quaternion.Euler(0, 0, 0), 12 * Time.deltaTime);
}
}
上面修改模型朝向时用的是本地旋转值。空手状态时,因为模型的父级朝向目标,移动时应该是相对于父级的本地方向而不是真实的方向。拔剑状态时模型应该朝向目标,因为父级朝向目标,只需要让模型旋转归零就能与父级同样朝向。
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Transform target;
///
/// 角色控制器
///
CharacterController characterController;
///
/// 动画控制器
///
PlayerAnimController animController;
///
/// 角色状态
///
CharacterState state;
///
/// 按键
///
[SerializeField] InputKeys keys;
///
/// 模型
///
Transform model;
///
/// 锁定状态下的摄像机
///
GameObject lockCamera;
///
/// 移动欲望
///
Vector3 moveDir;
private void Awake()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
characterController = GetComponent<CharacterController>();
animController = GetComponentInChildren<PlayerAnimController>();
animController.PlayerController = this;
state = new CharacterState();
animController.State = state;
model = transform.Find("Model");
lockCamera = GameObject.Find("Virtual Camera Group").transform.Find("LockLook").gameObject;
}
private void Update()
{
InputCheck();
}
private void FixedUpdate()
{
FreeLook();
LockLook();
Vector2 movement = SquareToCircle(moveDir);
if(state.isLocking && state.isEquiped)
{
animController.SetFloat("SpeedZ", movement.y);
animController.SetFloat("SpeedX", movement.x);
}
else
{
animController.SetFloat("SpeedZ", movement.sqrMagnitude);
}
}
///
/// 自由视角
///
void FreeLook()
{
if (state.isLocking)
return;
if (moveDir.sqrMagnitude < 0.1f)
return;
model.localRotation = Quaternion.Lerp(model.localRotation, Quaternion.Euler(0 ,0 ,0), 12 * Time.deltaTime);
Vector3 dir = moveDir.normalized;
float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg + Camera.main.transform.eulerAngles.y;
Quaternion targetDir = Quaternion.Euler(0, angle, 0);
transform.rotation = Quaternion.Lerp(transform.rotation, targetDir, 12 * Time.deltaTime);
}
///
/// 锁定视角
///
void LockLook()
{
if (!state.isLocking)
return;
if (moveDir.sqrMagnitude < 0.1f)
return;
Vector3 cameraEuler = Camera.main.transform.eulerAngles;
transform.rotation = Quaternion.Euler(0, cameraEuler.y, 0);
//非战斗状态
if(!state.isEquiped)
{
Vector3 dir = moveDir.normalized;
float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;
Quaternion targetDir = Quaternion.Euler(0, angle, 0);
model.localRotation = Quaternion.Lerp(model.localRotation, targetDir, 12 * Time.deltaTime);
}
//战斗状态
else
{
model.localRotation = Quaternion.Lerp(model.localRotation, Quaternion.Euler(0, 0, 0), 12 * Time.deltaTime);
}
}
///
/// 输入检测
///
void InputCheck()
{
float fwd = 0;
float right = 0;
fwd = Input.GetKey(keys.fwd) ? 1 : fwd;
fwd = Input.GetKey(keys.bwd) ? -1 : fwd;
right = Input.GetKey(keys.right) ? 1 : right;
right = Input.GetKey(keys.left) ? -1 : right;
moveDir.z = Mathf.Lerp(moveDir.z, fwd, 24 * Time.deltaTime);
moveDir.x = Mathf.Lerp(moveDir.x, right, 24 * Time.deltaTime);
//拔出武器
if (Input.GetKeyDown(keys.equip))
{
state.isEquiped = !state.isEquiped;
animController.SetTrigger("Equip");
}
//锁定视角
if(Input.GetKeyDown(keys.Lock))
{
state.isLocking = !state.isLocking;
lockCamera.SetActive(state.isLocking);
animController.SetBool("LockState", state.isLocking);
}
}
///
/// 椭圆映射
///
///
///
Vector2 SquareToCircle(Vector3 oldVec)
{
Vector2 newVec;
newVec.x = oldVec.x * Mathf.Sqrt(1 - (oldVec.z * oldVec.z) / 2);
newVec.y = oldVec.z * Mathf.Sqrt(1 - (oldVec.x * oldVec.x) / 2);
return newVec;
}
///
/// 动画根节点速度作为移动速度
///
///
public void SetCharacterVelocity(Vector3 velocity)
{
characterController.SimpleMove(new Vector3(velocity.x, characterController.velocity.y, velocity.z));
}
}
using UnityEngine;
public class PlayerAnimController : MonoBehaviour
{
///
/// 角色逻辑控制器
///
PlayerController playerController;
public PlayerController PlayerController { set { playerController = value; } }
///
/// 角色状态
///
CharacterState state;
public CharacterState State { set { state = value; } }
///
/// 动画状态机
///
Animator anim;
private void Awake()
{
anim = GetComponent<Animator>();
}
///
/// 设置Float参数
///
///
///
public void SetFloat(string name, float value)
{
anim.SetFloat(name, value);
}
///
/// 触发器
///
///
public void SetTrigger(string name)
{
anim.SetTrigger(name);
}
public void SetBool(string name, bool value)
{
anim.SetBool(name, value);
}
///
/// 动画执行时每一帧调用
///
private void OnAnimatorMove()
{
playerController.SetCharacterVelocity(anim.velocity);
}
#region 帧事件
///
/// 拔出武器调用
///
public void OnEquipStateEnter()
{
SetBool("IsEquiped", true);
}
///
/// 收起武器调用
///
public void OnUnEquipStateEnter()
{
SetBool("IsEquiped", false);
}
#endregion
}
工程文件下载