本文主要是介绍20240115-【UNITY 学习】第一人称移动增加斜坡移动、冲刺和蹲伏功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
直接修改或者替换PlayerMovement_01.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerMovement_02 : MonoBehaviour
{private float moveSpeed; // 玩家移动速度public float walkSpeed = 7; // 行走速度public float sprintSpeed = 10; // 冲刺速度public float groundDrag = 5; // 地面时的阻力public float playerHeight = 2; // 玩家身高public LayerMask whatIsGround; // 地面的LayerMaskprivate bool grounded; // 是否在地面上public float jumpForce = 6; // 跳跃力度public float jumpCooldown = 0.25f; // 跳跃冷却时间public float airMultiplier = 0.4f; // 空中移动速度衰减private bool readyToJump = true; // 是否可以跳跃public float crouchSpeed = 3.5f; // 蹲伏时的移动速度public float crouchYScale = 0.5f; // 蹲伏时的Y轴缩放比例private float startYScale; // 初始Y轴缩放比例public float maxSlopAngle = 40; // 最大坡度角度private RaycastHit slopeHit; // 坡度检测的射线信息private bool exitingSlope = true; // 是否正在离开坡度public KeyCode jumpKey = KeyCode.Space; // 跳跃键public KeyCode sprintKey = KeyCode.LeftShift; // 冲刺键public KeyCode crouchKey = KeyCode.LeftControl; // 下蹲键public Transform orientation; // 玩家朝向的Transformprivate float h; // 水平输入private float v; // 垂直输入private Vector3 moveDirection; // 移动方向private Rigidbody rb; // 玩家刚体public MovementState state; // 当前玩家的移动状态public enum MovementState{walking, // 行走sprinting, // 冲刺crouching, // 蹲伏air // 空中}private void Start(){rb = GetComponent<Rigidbody>();rb.freezeRotation = true; // 防止刚体旋转startYScale = transform.localScale.y; // 记录初始的Y轴缩放}private void Update(){grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);MyInput();SpeedControl();StateHandler();if (grounded)rb.drag = groundDrag;elserb.drag = 0;}private void FixedUpdate(){MovePlayer();}private void MyInput(){// 获取水平和垂直输入h = Input.GetAxisRaw("Horizontal");v = Input.GetAxisRaw("Vertical");// 如果按下跳跃键且准备好跳,并且在地面上if (Input.GetKey(jumpKey) && readyToJump && grounded){readyToJump = false;Jump();Invoke(nameof(ResetJump), jumpCooldown);}if (Input.GetKeyDown(crouchKey)){// 调整玩家缩放以模拟蹲下效果transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);}// 如果释放下蹲键if (Input.GetKeyUp(crouchKey)){// 恢复到原始Y轴缩放transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);}}private void MovePlayer(){// 根据朝向计算移动方向moveDirection = orientation.forward * v + orientation.right * h;// 如果在斜坡上并且不是即将离开斜坡if (OnSlope() && !exitingSlope){// 在斜坡上施加力,以便更好地移动rb.AddForce(GetSlopeMoveDirection() * moveSpeed * 20f, ForceMode.Force);// 如果垂直速度为正(上升),则额外施加向下的力,以克服斜坡引起的垂直速度变慢if (rb.velocity.y > 0){rb.AddForce(Vector3.down * 80f, ForceMode.Force);}}else if (grounded) // 如果在地面上{rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force); // 在地面上施加移动力}else if (!grounded) // 如果在空中{// 在空中施加移动力,乘以空中移动速度衰减系数rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);}// 根据是否在斜坡上决定是否启用重力rb.useGravity = !OnSlope();}private void SpeedControl(){// 如果在斜坡上并且不是即将离开斜坡if (OnSlope() && !exitingSlope){// 如果速度的大小超过了设定的移动速度if (rb.velocity.magnitude > moveSpeed){// 将速度归一化,并乘以设定的移动速度,以限制速度在设定范围内rb.velocity = rb.velocity.normalized * moveSpeed;}}// 如果不在斜坡上else{// 获取水平方向的速度Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);// 如果水平速度的大小超过了设定的移动速度if (flatVel.magnitude > moveSpeed){// 限制水平速度在设定范围内Vector3 limitedVel = flatVel.normalized * moveSpeed;// 更新刚体的速度,保持垂直速度不变rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);}}}private void Jump(){exitingSlope = true;//rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);rb.velocity = Vector3.zero;// 添加向上的力以实现跳跃rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);}private void ResetJump(){readyToJump = true;exitingSlope = false;}private void StateHandler(){// 如果在地面上并且按住冲刺键if (grounded && Input.GetKey(sprintKey)){// 设置当前状态为冲刺状态state = MovementState.sprinting;// 设置移动速度为冲刺速度moveSpeed = sprintSpeed;}// 如果在地面上但没有按住冲刺键else if (grounded){// 设置当前状态为行走状态state = MovementState.walking;// 设置移动速度为行走速度moveSpeed = walkSpeed;}// 如果不在地面上else{// 设置当前状态为空中状态state = MovementState.air;}// 如果按住蹲伏键if (Input.GetKey(crouchKey)){// 设置当前状态为蹲伏状态state = MovementState.crouching;// 设置移动速度为蹲伏速度moveSpeed = crouchSpeed;}}private bool OnSlope(){// 使用射线检测当前位置向下,获取击中信息存储在slopeHit中if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight * 0.5f + 0.3f)){// 计算斜坡的角度float angle = Vector3.Angle(Vector3.up, slopeHit.normal);// 如果角度小于最大允许斜坡角度且不等于0,表示在斜坡上return angle < maxSlopAngle && angle != 0;}// 如果没有击中信息,或者角度不符合条件,表示不在斜坡上return false;}// 获取在斜坡上移动的方向private Vector3 GetSlopeMoveDirection(){// 使用Vector3.ProjectOnPlane将移动方向投影到斜坡法线上,然后进行归一化return Vector3.ProjectOnPlane(moveDirection, slopeHit.normal).normalized;}
}
这篇关于20240115-【UNITY 学习】第一人称移动增加斜坡移动、冲刺和蹲伏功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!