赞
踩
Vector3 motionVector = Vector3.zero;
float horizontal_axis = Input.GetAxis("Horizontal");
float vertical_axis = Input.GetAxis("Vertical");
motionVector += playerTransform.forward * (moveSpeed * vertical_axis * Time.fixedDeltaTime);
motionVector += playerTransform.right * (moveSpeed * horizontal_axis * Time.fixedDeltaTime);
characterController.Move(motionVector);
verticalVelocity -= gravity * Time.fixedDeltaTime;
motionVector += Vector3.up * (verticalVelocity * Time.fixedDeltaTime);
isGround = Physics.CheckSphere(GroundCheckPoint.position, checkShereRadius, GroundLayer);
public float MaxJumpHeight = 5f;
if (isGround)
{
if (Input.GetButtonDown("Jump"))
{
//因为有最高的跳跃高度规定,用高中的公式来计算,上跳和下落就是对称的运动vt = gt; 2h/g开平方 = t,
//那么h高度时刻速度应该是vt = g*根号下(2h/g) = 根号下2gh
verticalVelocity = Mathf.Sqrt(2 * gravity * MaxJumpHeight);
//跳跃的时候应该记录一下起跳瞬间的水平方向的速度,保持不变
}
}
private float tmp_horizontal_axis;
private float tmp_vertical_axis;
if (isGround)
{
if (Input.GetButtonDown("Jump"))
{
tmp_horizontal_axis = horizontal_axis;
tmp_vertical_axis = vertical_axis;
//因为有最高的跳跃高度规定,用高中的公式来计算,上跳和下落就是对称的运动vt = gt; 2h/g开平方 = t,
//那么h高度时刻速度应该是vt = g*根号下(2h/g) = 根号下2gh
verticalVelocity = Mathf.Sqrt(2 * gravity * MaxJumpHeight);
//跳跃的时候应该记录一下起跳瞬间的水平方向的速度,保持不变
}
}
if (isGround)
{
motionVector += playerTransform.forward * (moveSpeed * vertical_axis * Time.fixedDeltaTime);
motionVector += playerTransform.right * (moveSpeed * horizontal_axis * Time.fixedDeltaTime);
}
else
{
//跳起来之后是有水平方向速度的
motionVector += playerTransform.forward * (moveSpeed * tmp_vertical_axis * Time.fixedDeltaTime);
motionVector += playerTransform.right * (moveSpeed * tmp_horizontal_axis * Time.fixedDeltaTime);
}
float horizontal_axis = 0f; float vertical_axis = 0f; if (isGround) { horizontal_axis = Input.GetAxis("Horizontal"); vertical_axis = Input.GetAxis("Vertical"); motionVector += playerTransform.forward * (moveSpeed * vertical_axis * Time.fixedDeltaTime); motionVector += playerTransform.right * (moveSpeed * horizontal_axis * Time.fixedDeltaTime); } else { //跳起来之后是有水平方向速度的 motionVector += playerTransform.forward * (moveSpeed * tmp_vertical_axis * Time.fixedDeltaTime); motionVector += playerTransform.right * (moveSpeed * tmp_horizontal_axis * Time.fixedDeltaTime); }
/// <summary> /// 角色移动和视角移动 /// </summary> public class PlayerMovementController : MonoBehaviour { [Space(20)] public float rotateSpeed = 180; [Range(1, 2)] public float rotateRatio = 1; public Transform playerTransform; public Transform eyeViewTransform; public float MaxViewAngle = 65f; private float tmp_viweRotationOffset; public float gravity = 9.8f; public float verticalVelocity = 0; public bool isGround = false; public LayerMask GroundLayer; public Transform GroundCheckPoint; public float checkShereRadius; public float MaxJumpHeight = 5f; private float tmp_horizontal_axis; private float tmp_vertical_axis; //move [Space(20)] public CharacterController characterController; public float moveSpeed = 10; private void Start() { characterController = this.GetComponent<CharacterController>(); } private void FixedUpdate() { PlayerRotateControl(); PlayerMoveControl(); } /** * 通过键盘控制角色移动 */ public void PlayerMoveControl() { if (characterController == null) { return; } Vector3 motionVector = Vector3.zero; //获取键盘输入 float horizontal_axis = 0f; float vertical_axis = 0f; //注意这里是对角色自身的方向来移动 //注意下面这个wasd方向的位移结构都是:1.方向单位向量 2.速度 3.输入 4.(非必须)时间间隔,换成0.02也是可以的 //注意一定是速度的累加,因为键盘输入的值是会变的,是慢慢增长的,松开是会回落的 if (isGround) { horizontal_axis = Input.GetAxis("Horizontal"); vertical_axis = Input.GetAxis("Vertical"); motionVector += playerTransform.forward * (moveSpeed * vertical_axis * Time.fixedDeltaTime); motionVector += playerTransform.right * (moveSpeed * horizontal_axis * Time.fixedDeltaTime); } else { //跳起来之后是有水平方向速度的 motionVector += playerTransform.forward * (moveSpeed * tmp_vertical_axis * Time.fixedDeltaTime); motionVector += playerTransform.right * (moveSpeed * tmp_horizontal_axis * Time.fixedDeltaTime); } //跳跃和下落 //垂直方向应该取得是视界坐标的朝向 //这里需要自由落体的公式 h = 0.5 * g * t * t 因此 下落的所读就是 Vt = g * t //先实现重力 , 垂直方向的速度是累加的! verticalVelocity -= gravity * Time.fixedDeltaTime; motionVector += Vector3.up * (verticalVelocity * Time.fixedDeltaTime); //在角色接触到地面的时候,向下的速度归零 //地面的碰撞检测可以使用,碰撞,也可以使用射线,这里使用一个checkPoint,放在胶囊体下方的原型截面线上 //CheckSphere检车某个点为圆心的球体内是否有碰撞体,加入 verticalVelocity < 0 判断是防止跳跃的时候碰到物体,但是跳跃的动作还没结束,V就强制为0了 isGround = Physics.CheckSphere(GroundCheckPoint.position, checkShereRadius, GroundLayer); if (isGround && verticalVelocity < 0) { isGround = true; verticalVelocity = 0; } //写跳跃的脚本 if (isGround) { if (Input.GetButtonDown("Jump")) { tmp_horizontal_axis = horizontal_axis; tmp_vertical_axis = vertical_axis; //因为有最高的跳跃高度规定,用高中的公式来计算,上跳和下落就是对称的运动vt = gt; 2h/g开平方 = t, //那么h高度时刻速度应该是vt = g*根号下(2h/g) = 根号下2gh verticalVelocity = Mathf.Sqrt(2 * gravity * MaxJumpHeight); //跳跃的时候应该记录一下起跳瞬间的水平方向的速度,保持不变 } } characterController.Move(motionVector); } /** * 鼠标控制角色视角移动 */ private void PlayerRotateControl() { if (playerTransform == null || eyeViewTransform == null) { return; } //x控制的是水平方向旋转,也就是控制沿Y轴旋转,控制Player的水平方向转动 float offset_x = Input.GetAxis("Mouse X"); //控制垂直方向旋转,也就是沿x轴旋转,这里只控制EYE_VIEW照相机在垂直方向的转动 float offset_y = Input.GetAxis("Mouse Y"); playerTransform.Rotate(Vector3.up * (offset_x * rotateSpeed * rotateRatio * Time.fixedDeltaTime)); //对旋转角进行累加才能够进行对最大角度的限制 tmp_viweRotationOffset -= offset_y * rotateSpeed * rotateRatio * Time.fixedDeltaTime; //限制一下旋转的角度 tmp_viweRotationOffset = Mathf.Clamp(tmp_viweRotationOffset, -MaxViewAngle, MaxViewAngle); Quaternion EyeLocalQuaternion = Quaternion.Euler(new Vector3(tmp_viweRotationOffset, eyeViewTransform.localEulerAngles.y, eyeViewTransform.localEulerAngles.z)); //注意旋转角是四元素,需要用四元素处理 eyeViewTransform.localRotation = EyeLocalQuaternion; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。