项目实训(十)FPS游戏之移动,跑步,跳跃,下蹲等


前言

FPS游戏之移动,跑步,跳跃,下蹲等
FPCharacterControllerMovement 该脚本用于获取玩家的输入行为,根据相应的按键输入等对角色的移动进行控制。


一、如何让角色移动

首先判断是否在地面

if (characterController.isGrounded)
        {
            var tmp_Horizontal = Input.GetAxis("Horizontal");
            var tmp_Vertical = Input.GetAxis("Vertical");
            movementDirection =
                characterTransform.TransformDirection(new Vector3(tmp_Horizontal, 0, tmp_Vertical)).normalized;

用move函数设置物体的重力

 movementDirection.y -= Gravity * Time.deltaTime;
        var tmp_Movement = CurrentSpeed * Time.deltaTime * movementDirection;
        characterController.Move(tmp_Movement);

二、如何让角色按 shift 键奔跑

在unity中修改速度,判断是否按左边的shift键,如果按了则执行经改造后的增快的速度
请添加图片描述

if (isCrouched)
            {
                CurrentSpeed = Input.GetKey(KeyCode.LeftShift) ? SprintingSpeedWhenCrouched : WalkSpeedWhenCrouched;
            }
            else
            {
                CurrentSpeed = Input.GetKey(KeyCode.LeftShift) ? SprintingSpeed : WalkSpeed;
            }

三、如何让角色按“C”键下蹲

1.需要使用携程函数

协程:协程是一个分部执行,遇到条件(yield return 语句)

时才会挂起,直到条件满足才会被唤醒继续执行后面的代码。

 private IEnumerator DoCrouch(float _target)
    {
        float tmp_CurrentHeight = 0;
        while (Mathf.Abs(characterController.height - _target) > 0.1f)
        {
            yield return null;
            characterController.height =
                Mathf.SmoothDamp(characterController.height, _target,
                    ref tmp_CurrentHeight, Time.deltaTime * 5);
        }
    }

2.判断是否下蹲

判断是否蹲下,将 isCrouched 重置为对立状态,按下C键后,如若为下蹲状态,则可获取到下蹲状态的高度,并将其制为下蹲状态

 if (Input.GetKeyDown(KeyCode.C))
            {
                var tmp_CurrentHeight = isCrouched ? originHeight : CrouchHeight;
                StartCoroutine(DoCrouch(tmp_CurrentHeight));
                isCrouched = !isCrouched;
            }

四、如何让角色按空格键下蹲

 if (Input.GetButtonDown("Jump"))
            {
                movementDirection.y = JumpHeight;
            }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编写FPS角色的移动跳跃、射击可以使用游戏引擎来实现,以下是基于Unity引擎的示例代码: 移动: ```csharp public class PlayerMovement : MonoBehaviour { public float speed = 10f; // 玩家移动速度 private void Update() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized; // 获取玩家移动方向 transform.Translate(direction * speed * Time.deltaTime); // 移动玩家 } } ``` 跳跃: ```csharp public class PlayerJump : MonoBehaviour { public float jumpForce = 5f; // 玩家跳跃力度 public float groundDistance = .2f; // 地面检测距离 public LayerMask groundMask; // 地面的层级 private Rigidbody rb; private bool isGrounded = true; private void Start() { rb = GetComponent<Rigidbody>(); } private void Update() { isGrounded = Physics.CheckSphere(transform.position, groundDistance, groundMask); // 检测玩家是否在地面上 if (isGrounded && Input.GetKeyDown(KeyCode.Space)) // 按下空格键并且在地面上 { rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); // 给玩家添加向上的力 isGrounded = false; } } } ``` 射击: ```csharp public class PlayerShoot : MonoBehaviour { public GameObject bulletPrefab; // 子弹预制体 public Transform firePoint; // 发射点 public float bulletSpeed = 10f; // 子弹速度 private void Update() { if (Input.GetButtonDown("Fire1")) // 按下鼠标左键 { GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); // 创建子弹 Rigidbody bulletRb = bullet.GetComponent<Rigidbody>(); bulletRb.velocity = firePoint.forward * bulletSpeed; // 子弹向前发射 } } } ``` 以上是基于Unity引擎的示例代码,不同的游戏引擎实现方式可能会有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值