【发布时间】:2022-01-21 17:00:01
【问题描述】:
我正在尝试创建一个 FPS 游戏,并且大部分内容都涵盖了。
- 步行
- 短跑
- 蹲下
- 跳跃
- 重力
但是,我正在尝试实现一个系统,在该系统中,每当您按住 LeftShift 按钮时,它也会播放此冲刺音频。
问题是,每当我按住 Shift 按钮时,我都在跑步,但直到我松开 Shift 按钮后音频才会播放。
我还没有找到与我有类似问题的任何其他论坛。我想知道是否有人可以帮助我确定问题?谢谢你,如果答案很明显,我很抱歉,我很新。
using UnityEngine;
使用 System.Collections;
公开课运动:MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float jumpHeight = 3f;
//Gravity
public float gravity = -9.81f;
Vector3 velocity;
//Sprinting
public bool isSprinting;
public float sprintingMultiplier;
//Crouching
public bool isCrouching;
public float crouchingMultiplier;
public float crouchingHeight = 2f;
public float standingHeight = 4f;
//Ground check
bool isGrounded;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
void Update()
{
//Ground check
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
//Walking
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * y;
//Jumping
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
//Sprinting
if (Input.GetKey(KeyCode.LeftShift))
{
isSprinting = true;
}
else
{
isSprinting = false;
}
if (isSprinting == true)
{
move *= sprintingMultiplier;
FindObjectOfType<AudioManager>().Play("Run");
}
//Crouching
if (Input.GetKey(KeyCode.C))
{
isCrouching = true;
}
else
{
isCrouching = false;
}
if (isCrouching == true)
{
controller.height = crouchingHeight;
move *= crouchingMultiplier;
}
else
{
controller.height = standingHeight;
}
//Gravity
velocity.y += gravity * Time.deltaTime;
controller.Move(move * speed * Time.deltaTime);
controller.Move(velocity * Time.deltaTime);
}
}
【问题讨论】: