using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
[RequireComponent(typeof (AudioSource))]
public class FirstPersonController : MonoBehaviour
{
[SerializeField] private bool m_IsWalking;
[SerializeField] private float m_WalkSpeed;
[SerializeField] private float m_RunSpeed;
[SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
[SerializeField] private float m_JumpSpeed;
[SerializeField] private float m_StickToGroundForce;//向下的速度
[SerializeField] private float m_GravityMultiplier;
[SerializeField] private MouseLook m_MouseLook;
[SerializeField] private bool m_UseFovKick;
[SerializeField] private FOVKick m_FovKick = new FOVKick();
[SerializeField] private bool m_UseHeadBob;
[SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
[SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
[SerializeField] private float m_StepInterval;
[SerializeField] private AudioClip[] m_FootstepSounds; // an array of footstep sounds that will be randomly selected from.
[SerializeField] private AudioClip m_JumpSound; // 角色跳跃的音乐
[SerializeField] private AudioClip m_LandSound; // 角色着陆的音乐
private Camera m_Camera;//摄像机组件
private bool m_Jump;
private float m_YRotation;//
private Vector2 m_Input;//输入
private Vector3 m_MoveDir = Vector3.zero;//移动方向及速度
private CharacterController m_CharacterController;//自身的角色控制器
private CollisionFlags m_CollisionFlags;//
private bool m_PreviouslyGrounded;//上帧是否着陆
private Vector3 m_OriginalCameraPosition;//摄像机的初始位置
private float m_StepCycle;
private float m_NextStep;
private bool m_Jumping;//是否起跳
private AudioSource m_AudioSource;//音源组件
private void Start()
{
m_CharacterController = GetComponent
m_Camera = Camera.main;
m_OriginalCameraPosition = m_Camera.transform.localPosition;
m_FovKick.Setup(m_Camera);
m_HeadBob.Setup(m_Camera, m_StepInterval);
m_StepCycle = 0f;
m_NextStep = m_StepCycle/2f;
m_Jumping = false;
m_AudioSource = GetComponent
m_MouseLook.Init(transform , m_Camera.transform);
}
private void Update()
{
RotateView();
// the jump state needs to read here to make sure it is not missed
if (!m_Jump)//没有起跳
{
m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");//如果Jump按下m_Jump为true
}
//刚着陆的情况
if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)//m_PreviouslyGrounded==false
{
StartCoroutine(m_JumpBob.DoBobCycle());
PlayLandingSound();
m_MoveDir.y = 0f;
m_Jumping = false;
}
//刚起跳
if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
{
m_MoveDir.y = 0f;
}
m_PreviouslyGrounded = m_CharacterController.isGrounded;//跟新上帧着陆标志位
}
//着陆播放音乐
private void PlayLandingSound()
{
m_AudioSource.clip = m_LandSound;
m_AudioSource.Play();
m_NextStep = m_StepCycle + .5f;
}
private void FixedUpdate()
{
float speed;
GetInput(out speed);//根据输入得到速度
//方向永远随着摄像机朝向的正方向
Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;//因为Move为世界坐标,所以需用自身的方向
RaycastHit hitInfo;//碰撞信息
Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
m_CharacterController.height/2f);//球星检测,就相当于从从胶囊体的中心放一个球,向下扔,看有没有碰到什么东西
//将一个向量投射到一个垂直于平面的发线所定义的平面上,如果路有斜坡,角色的Y轴就有速度
desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;//只要方向
m_MoveDir.x = desiredMove.x*speed;//确定XZ轴的速度
m_MoveDir.z = desiredMove.z*speed;
if (m_CharacterController.isGrounded)//如果在地面上
{
m_MoveDir.y = -m_StickToGroundForce;//给Y轴向下的速度,防止物体悬空
if (m_Jump)//要跳跃
{
m_MoveDir.y = m_JumpSpeed;//给Y轴一个向上的速度
PlayJumpSound();//播放跳跃音乐
m_Jump = false;//设置跳跃标志位为false
m_Jumping = true;//正在跳跃
}
}
else//不在地面上
{
m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;//每帧作用一个2倍的重力
}
m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);//移动,返回碰撞信息
ProgressStepCycle(speed);//处理脚布的声音
UpdateCameraPosition(speed);//跟新摄像机的摆动(位置)
}
///
/// 播放跳跃音乐
///
private void PlayJumpSound()
{
m_AudioSource.clip = m_JumpSound;
m_AudioSource.Play();
}
///
/// 根据速度处理脚布的声音
///
///
private void ProgressStepCycle(float speed)
{
//在移动情况下,增加一个时间增量
if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))//判断正在移动
{
m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
Time.fixedDeltaTime;
}
if (!(m_StepCycle > m_NextStep))//时间没有到达跳出
{
return;
}
m_NextStep = m_StepCycle + m_StepInterval;//时间到达了
PlayFootStepAudio();//播放一个脚步的声音
}
///
/// 脚步声音
///
private void PlayFootStepAudio()
{
if (!m_CharacterController.isGrounded)//不在地面上,返回不播放
{
return;
}
// 从数组中随机播放一个声音,不包括0
int n = Random.Range(1, m_FootstepSounds.Length);//得到一个随机索引
m_AudioSource.clip = m_FootstepSounds[n];//得到随机的脚步声音片段
m_AudioSource.PlayOneShot(m_AudioSource.clip);//播放一次
// 把播放的声音放在0处,下次不会在播放(交换元素)
m_FootstepSounds[n] = m_FootstepSounds[0];
m_FootstepSounds[0] = m_AudioSource.clip;
}
///
/// 跟新摄像机的位置
///
///
private void UpdateCameraPosition(float speed)
{
Vector3 newCameraPosition;//存储跟新位置
if (!m_UseHeadBob)//不使用头部摇摆的情况下(默认=true)
{
return;
}
if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)//速度向量长度大于0,在地面上
{
m_Camera.transform.localPosition =
m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
(speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
newCameraPosition = m_Camera.transform.localPosition;
newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
}
else
{
newCameraPosition = m_Camera.transform.localPosition;
newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
}
m_Camera.transform.localPosition = newCameraPosition;
}
///
/// 获取输入,计算速度和方向
///
///
private void GetInput(out float speed)
{
// 获取输入
float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
float vertical = CrossPlatformInputManager.GetAxis("Vertical");
bool waswalking = m_IsWalking;//是否在行走(false)
#if !MOBILE_INPUT
// 在PC平台上运行,根据Left Shift按键改变移动状态(行走,奔跑)
m_IsWalking = !Input.GetKey(KeyCode.LeftShift);//没有按下Left Shift,true,按下false
#endif
// 设置行走和run的速度
speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
m_Input = new Vector2(horizontal, vertical);
//如果长度大于一,归1话(只要方向)
if (m_Input.sqrMagnitude > 1)
{
m_Input.Normalize();
}
//根据摄像机的角度,处理速度,只有人在奔跑的时候才处理
// handle speed change to give an fov kick
// only if the player is going to a run, is running and the fovkick is to be used
if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
{
StopAllCoroutines();
StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());//奔跑开
}
}
///
/// 处理鼠标移动后摄像机的位置和角度
///
private void RotateView()
{
m_MouseLook.LookRotation (transform, m_Camera.transform);
}
///
/// 处理角色的碰撞
///
///
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;//碰撞器所附加的刚体
//dont move the rigidbody if the character is on top of it//刚体在下面不会移动
if (m_CollisionFlags == CollisionFlags.Below)
{
return;
}
if (body == null || body.isKinematic)//刚体为空或不受物理影响时不出来
{
return;
}
body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);//给人一个方向的冲力,反弹作用
}
}
}