【问题标题】:Audio won't play until after I let go of key放开按键后音频才会播放
【发布时间】: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);


}

}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    在不知道AudioManager 的类型以及Play 到底是什么的情况下,我只能猜测它是在某个使用AudioSource 的地方。

    请注意,AudioSource.Play 将始终(重新)启动音频剪辑。

    如果将AudioSource.clip 设置为正在播放的同一剪辑,则剪辑听起来像是重新开始。 AudioSource 将假定任何 Play 调用都会有一个新的音频剪辑要播放。

    因此,由于您每一帧都调用了该行,因此您不断地重新启动该剪辑->它只能在您放开键并且不再重新启动后才能最终播放到结束。


    因此,您要么希望确保仅通过 GetButtonDown 播放声音,例如而是使用PlayOneShot,这样即使同时开始另一个声音,声音也会完全播放一次。

    或者您必须确保检查当前正在播放哪个剪辑,如果同一剪辑已经在播放,则跳过Play 调用。

    这两种情况都必须在您没有向我们展示的 AudioManager 实现中发生...

    【讨论】:

      猜你喜欢
      • 2022-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多