您的位置:首页 > 移动开发 > Unity3D

unity标准资源包FirstPersonController的分析

2017-05-29 14:54 781 查看
1.鼠标转动
基类MouseLook,负责处理鼠标转向和准星是否锁定,这个比较简单

using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace UnityStandardAssets.Characters.FirstPerson
{
[Serializable]
public class MouseLook
{
public float XSensitivity = 2f; //x轴灵敏度
public float YSensitivity = 2f; //y轴灵敏地
public bool clampVerticalRotation = true; //是否限制俯仰角
public float MinimumX = -90F; //最小俯角
public float MaximumX = 90F; //最大仰角
public bool smooth;
public float smoothTime = 5f;
public bool lockCursor = true; //锁定准星

private Quaternion m_CharacterTargetRot; //这个是角色的属性
private Quaternion m_CameraTargetRot; //这个是摄像机的属性,这里有必要说下,
//这里鼠标控制的横向转动是角色转,角色带动摄像机转,但竖着转动是摄像机转,角色不动
//这样就可以避免出现角色扭曲的情况
private bool m_cursorIsLocked = true;

public void Init(Transform character, Transform camera)
{
m_CharacterTargetRot = character.localRotation;
m_CameraTargetRot = camera.localRotation;
}

public void LookRotation(Transform character, Transform camera)
{
float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;//x和y轴的转动角度

m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f); //数学关系,不懂= =
m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);

if(clampVerticalRotation)
m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot); //得到俯仰角

if(smooth) //平滑鼠标,第一人称大部分都不需要,所以不看这个
{
character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
smoothTime * Time.deltaTime);
camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
smoothTime * Time.deltaTime);
}
else
{
character.localRotation = m_CharacterTargetRot; //更新坐标,发现这里更新的是两部分,也就是角色和摄像机
camera.localRotation = m_CameraTargetRot;
}

UpdateCursorLock(); //处理准星是否锁定的问题
}

public void SetCursorLock(bool value)
{
lockCursor = value;
if(!lockCursor)
{//we force unlock the cursor if the user disable the cursor locking helper
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}

public void UpdateCursorLock()
{
//if the user set
4000
"lockCursor" we check & properly lock the cursos
if (lockCursor)
InternalLockUpdate();
}

private void InternalLockUpdate()//如果要锁定准星,执行这个方法
{
if(Input.GetKeyUp(KeyCode.Escape))
{
m_cursorIsLocked = false; //按下Esc取消锁定
}
else if(Input.GetMouseButtonUp(0))
{
m_cursorIsLocked = true;
}

if (m_cursorIsLocked) //锁定
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else if (!m_cursorIsLocked) //取消锁定
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
//个人感觉这个m_cursorIsLocked没有必要.....
}

Quaternion ClampRotationAroundXAxis(Quaternion q)//Clamp俯仰角
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;

float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);

angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);

q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);

return q;
}

}
}主要功能在LookRotation()方法上,大体思路就是通过输入得到x和y的转动角,再通过ClampRotationAroundXAxis()方法限制一下俯仰角,最后判断何时锁定准星就可以了。注意这里横着是角色转,带动摄像机转,竖着是摄像机转,角色不转。
在主类FirstPersonController的Update()方法下运行(LookRotation被封装在RotateView()中),在FixUpdated()方法下运行UpdateCursorLock()方法

2.移动,跳跃,人物碰撞

在主类FirstPersonController中,运动之类的都放到FixedUpdate()中,检测跳跃按钮是否按下的在Update()方法里,通过GetInput()方法获取到输入,判断好是走还是跑,然后在FixedUpdate()里通过m_CharacterController.Move()方法让角色运动起来,并通过isGrounded属性判断跳跃的情况,应该有个用球形碰撞检测斜坡的,但是暂时没看懂,碰撞到其他物体后通过OnControllerColliderHit()回调函数执行给碰到的物体的加力的操作。

先给出FirstPersonController类的变量定义和初始化

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; // the sound played when character leaves the ground.
[SerializeField] private AudioClip m_LandSound; // the sound played when character touches back on ground.

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<CharacterController>(); //加载角色控制器
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; //跳跃状态=false
m_AudioSource = GetComponent<AudioSource>();
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");  //按下跳跃键
}

if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)//处理刚落地的情况
{
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 FixedUpdate()
{
float speed;     //定义速度,行走还是跑动两个速度二选一
GetInput(out speed);   //键盘输入并决定是跑还是走,将移动方向存到m_Input中,注意跳的输入放在了Update()里
// always move along the camera forward as it is the direction that it being aimed at
Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;  //将m_Input从二维转存到三维中去

// get a normal for the surface that is being touched to move along it
RaycastHit hitInfo;
/*
* Physics.SphereCast, 进行一次球形的碰撞
* param:
*  origin, 触碰的起始点
*  radius, 球形的半径
*  direction, 碰撞的方向
*  hitInfo, 碰撞的结果
*  maxDistance, 碰撞到的最大距离
*  layerMask, 碰撞层,所有的都可以碰撞
*  queryTriggerInteraction, 是否要触发triiger
*/
Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
m_CharacterController.height/2f, Physics.AllLayers, QueryTriggerInteraction.Ignore);
//进行球形碰撞,碰撞到的信息存储到了hitInfo中
desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
//看不懂

m_MoveDir.x = desiredMove.x*speed;
m_MoveDir.z = desiredMove.z*speed;
if (m_CharacterController.isGrounded)
{
m_MoveDir.y =-m_StickToGroundForce;

if (m_Jump)//跳跃情况
{
m_MoveDir.y = m_JumpSpeed;//跳跃速度
PlayJumpSound();//播放声音
m_Jump = false;
m_Jumping = true;
}
}
else
{
m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;//不在地上的时候收到重力作用
}
m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);  //通过CharacterController类的Move()方法移动
//move方法不处理重力
//m_CollisionFlags是一个存储着在路上碰撞到物体跟角色相对位置的枚举变量
ProgressStepCycle(speed);   //处理脚步声
UpdateCameraPosition(speed);//处理镜头晃动

m_MouseLook.UpdateCursorLock();  //处理准星锁定非锁定
}
private void GetInput(out float speed)
{
// Read input
float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
float vertical = CrossPlatformInputManager.GetAxis("Vertical");

bool waswalking = m_IsWalking; //定义一个正在walking的bool值

#if !MOBILE_INPUT
// On standalone builds, walk/run speed is modified by a key press.
// keep track of whether or not the character is walking or running
m_IsWalking = !Input.GetKey(KeyCode.LeftShift);  //按住shift让m_IsWalking转换成跑步状态
#endif
// set the desired speed to be walking or running
speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;   //按照跑动还是行走的逻辑决定速度
m_Input = new Vector2(horizontal, vertical);

// normalize input if it exceeds 1 in combined length:
if (m_Input.sqrMagnitude > 1)  //如果移动二维向量的模大于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 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);//给碰到的物体加力
}
3.镜头晃动

镜头晃动共有两种情况,一种是刚落地的时候镜头会往下压调用LerpControlledBob类对象,第二种是跑动或者行走的时候镜头的正常晃动调用CurveControlledBob类对象。

刚落地的情况是在Update()方法下的if (!m_PreviouslyGrounded && m_CharacterController.isGrounded){}条件下调用StartCoroutine(m_JumpBob.DoBobCycle());
using System;
using System.Collections;
using UnityEngine;

namespace UnityStandardAssets.Utility
{
[Serializable]
public class LerpControlledBob
{
public float BobDuration;   //晃动持续时间
public float BobAmount;    //往下晃动的深度

private float m_Offset = 0f;

// provides the offset that can be used
public float Offset()
{
return m_Offset;
}

public IEnumerator DoBobCycle()
{
// make the camera move down slightly
//下降
float t = 0f;
while (t < BobDuration)//计时器
{
m_Offset = Mathf.Lerp(0f, BobAmount, t/BobDuration);//计算补偿
t += Time.deltaTime;
yield return new WaitForFixedUpdate();//等待FixedUpdate()方法执行完
}

// make it move back to neutral
//上升恢复
t = 0f;
while (t < BobDuration)
{
m_Offset = Mathf.Lerp(BobAmount, 0f, t/BobDuration);
t += Time.deltaTime;
yield return new WaitForFixedUpdate();
}
m_Offset = 0f;
}
}
}
行走跑步晃动的是FixedUpdate()方法下的UpdateCameraPosition(speed);

这个方法也会影响跳跃晃动
private void UpdateCameraPosition(float speed)
{
Vector3 newCameraPosition;   //定义摄像机要移动到的位置
if (!m_UseHeadBob)
{
return;
}
if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)//玩家在地上运动的时候
{
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;
}
4.脚步声

脚步声分两种,一个是跳跃,一个是走跑

用AudioClip播放

着陆是在Update()方法下的PlayLandingSound(),跳跃是在FixedUpdate()下的PlayJumpSound();比较好找

走跑是在FixedUpdate()下的ProgressStepCycle(speed)
private void PlayJumpSound()
{
m_AudioSource.clip = m_JumpSound;
m_AudioSource.Play();
}
private void PlayLandingSound()
{
m_AudioSource.clip = m_LandSound;
m_AudioSource.Play();
m_NextStep = m_StepCycle + .5f;
}
private void PlayFootStepAudio()
{
if (!m_CharacterController.isGrounded)
{
return;
}
// pick & play a random footstep sound from the array,
// excluding sound at index 0
int n = Random.Range(1, m_FootstepSounds.Length);
m_AudioSource.clip = m_FootstepSounds
;
m_AudioSource.PlayOneShot(m_AudioSource.clip);
// move picked sound to index 0 so it's not picked next time
m_FootstepSounds
= m_FootstepSounds[0];
m_FootstepSounds[0] = m_AudioSource.clip;
}脚步声通过ProgressStepCycle()调用
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();
}
5FOV

目前我只发现奔跑的时候Fov会变大,行走时恢复,没看到有其他的效果

通过FOVKick类对象在主类的GetInput()方法调用StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
using System;
using System.Collections;
using UnityEngine;

namespace UnityStandardAssets.Utility
{
[Serializable]
public class FOVKick
{
public Camera Camera;                           // optional camera setup, if null the main camera will be used
[HideInInspector] public float originalFov;     // the original fov
public float FOVIncrease = 3f;                  // the amount the field of view increases when going into a run
public float TimeToIncrease = 1f;               // the amount of time the field of view will increase over
a5b9

public float TimeToDecrease = 1f;               // the amount of time the field of view will take to return to its original size
public AnimationCurve IncreaseCurve;

public void Setup(Camera camera)
{
CheckStatus(camera);

Camera = camera;
originalFov = camera.fieldOfView;
}

private void CheckStatus(Camera camera)
{
if (camera == null)
{
throw new Exception("FOVKick camera is null, please supply the camera to the constructor");
}

if (IncreaseCurve == null)
{
throw new Exception(
"FOVKick Increase curve is null, please define the curve for the field of view kicks");
}
}

public void ChangeCamera(Camera camera)
{
Camera = camera;
}

public IEnumerator FOVKickUp()
{
float t = Mathf.Abs((Camera.fieldOfView - originalFov)/FOVIncrease);
while (t < TimeToIncrease)
{
Camera.fieldOfView = originalFov + (IncreaseCurve.Evaluate(t/TimeToIncrease)*FOVIncrease);
t += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
}

public IEnumerator FOVKickDown()
{
float t = Mathf.Abs((Camera.fieldOfView - originalFov)/FOVIncrease);
while (t > 0)
{
Camera.fieldOfView = originalFov + (IncreaseCurve.Evaluate(t/TimeToDecrease)*FOVIncrease);
t -= Time.deltaTime;
yield return new WaitForEndOfFrame();
}
//make sure that fov returns to the original size
Camera.fieldOfView = originalFov;
}
}
}
通过计时器,方法类似,就不说了

总体来说,就是通过FirstPersonController类

调用MouseLook类对象处理鼠标转向

调用CharacterController类对象处理运动碰撞

调用LerpControlledBob和CurveControlledBob类对象处理摄像机晃动

调用AudioSource类对象处理声音

调用FOVKick类对象处理Fov变化

剩下的可以再细细研究了

本来我想比着这个轮子再造一个更清晰点的,把CharacterController类的调用再单独封装到一个类中,结果却发现在基类里调用CharacterController得不到,空指针,在面板上序列化才可以使用,但是每次都得开始游戏后在scene界面手动给它赋值,可能是因为我的对象是new出来的吧,不用new了也是不行,所以后来干脆放弃了(白琢磨几百行代码了T_T)。越来越感觉unity的c#跟平时用到的c#不能一样用,必定有些坑等着我们踩。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: