一、导入插件Cinemachine
重命名为ThirdPersonCamera
Follow和LookAt 选择为player
镜像Y轴,取消X轴
摄像机绑定模式为World Space
二、挂载代码PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
//记录轴向值
private float InputH;
private float InputV;
private Vector3 Direction;//记录移动方向
public CharacterController Controller1;
//玩家速度
public float MoveSpeed=10;
//玩家相机
public Camera Camera1;
//计算旋转角度引用
private float TurnSmooth;//旋转角度
public float TurnTime = 0.1f;//旋转时长
private void Start()
{
}
private void Update()
{
Move();
}
/// <summary>
/// 移动方法
/// </summary>
public void Move()
{
//获取水平和垂直
InputH = Input.GetAxis("Horizontal");
InputV = Input.GetAxis("Vertical");
Direction = new Vector3(InputH, 0, InputV);
if (Direction.magnitude>=0.1f)//检测是否有玩家输入
{
移动
//Controller1.Move(Direction*MoveSpeed*Time.deltaTime);
//计算目标角度
float TargetAngle = Mathf.Atan2(Direction.x, Direction.z) * Mathf.Rad2Deg
+ Camera1.transform.eulerAngles.y;//弧度值转为角度值
//平滑角度过渡
float Angle = Mathf.SmoothDampAngle(this.transform.eulerAngles.y, TargetAngle,
ref TurnSmooth, TurnTime);
//设置角色旋转
this.transform.rotation = Quaternion.Euler(0, Angle, 0);
//计算新的移动方向
Vector3 MoveDirection = Quaternion.Euler(0, TargetAngle, 0) * Vector3.forward;
//移动角色
Controller1.Move(MoveDirection * MoveSpeed * Time.deltaTime);
}
}
}