gpt4 book ai didi

c# - 播放器与具有特定标签的对象碰撞时的 Unity3D 播放声音

转载 作者:行者123 更新时间:2023-12-02 22:10:50 28 4
gpt4 key购买 nike

我使用 Unity 2019.2.14f1 创建一个简单的 3D 游戏。

在那个游戏中,我想在我的 Player 与具有特定标签的游戏​​对象发生碰撞时播放声音。

MainCamera 有一个音频监听器,我正在使用 Cinemachine Free Look,它跟随我的头像,在 ThridPersonController 内(我使用的是 Standard Assets 上的那个 - 但我隐藏了 Ethan 并添加了我自己的角色/头像)。

带有我要销毁的标签的游戏​​对象有一个音频源:

Audio Source on the GameObject with the specific tag

为了让声音在碰撞中播放,我首先创建了一个空的游戏对象作为 AudioManager,并向其中添加了一个新组件(C# 脚本):

using UnityEngine.Audio;
using System;
using UnityEngine;

public class AudioManager : MonoBehaviour
{

public Sound[] sounds;

// Start is called before the first frame update
void Awake()
{
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;

s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
}

// Update is called once per frame
public void Play (string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.Play();
}
}

并创建了脚本 Sound.cs:
using UnityEngine.Audio;
using UnityEngine;

[System.Serializable]
public class Sound
{
public string name;

public AudioClip clip;

[Range(0f, 1f)]
public float volume;
[Range(.1f, 3f)]
public float pitch;

[HideInInspector]
public AudioSource source;
}

之后,在 Unity UI 中,我转到 gameObject AudioManager 中的 Inspector,并在脚本中添加了一个新元素,我命名为:CatchingPresent。

AudioManager - Sound that I want to play when the player collides with an object.

在第三人称角色脚本中,为了在碰撞时销毁游戏对象(带有特定标签),我添加了以下内容:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(other.gameObject);
count = count - 1;
SetCountText();

}
}

它正常工作,因为该特定对象在碰撞时消失。现在,为了在播放器与带有标签的对象碰撞时播放“CatchingPresent”声音,在这种情况下, Present ,我尝试将以下内容添加到 ifOnCollisionEnter :
  • FindObjectOfType<AudioManager>().Play("CatchingPresent");

  • 但我收到错误:

    The type or namespace name 'AudioManager' could not be found (are you missing a using directive or an assembly reference?)


  • AudioManager.instance.Play("CatchingPresent");

  • 但我收到错误:

    The name 'AudioManager' does not exist in the current context



    由于所有编译器错误都需要在进入 Playmode 之前修复,任何有关如何在玩家和带有标签 Present 的游戏对象碰撞后播放声音的指导。被赞赏。

    编辑 1:假设它有帮助,这里是完整的 ThirdPersonUserControl.cs:
    using System;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityStandardAssets.CrossPlatformInput;

    namespace UnityStandardAssets.Characters.ThirdPerson
    {
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class ThirdPersonUserControl : MonoBehaviour
    {

    public Text countText;
    public Text winText;

    private int count;
    private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
    private Transform m_Cam; // A reference to the main camera in the scenes transform
    private Vector3 m_CamForward; // The current forward direction of the camera
    private Vector3 m_Move;
    private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input.


    private void Start()
    {

    count = 20;
    SetCountText();
    winText.text = "";

    // get the transform of the main camera
    if (Camera.main != null)
    {
    m_Cam = Camera.main.transform;
    }
    else
    {
    Debug.LogWarning(
    "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
    // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
    }

    // get the third person character ( this should never be null due to require component )
    m_Character = GetComponent<ThirdPersonCharacter>();
    }


    private void Update()
    {
    if (!m_Jump)
    {
    m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    }
    }


    // Fixed update is called in sync with physics
    private void FixedUpdate()
    {
    // read inputs
    float h = CrossPlatformInputManager.GetAxis("Horizontal");
    float v = CrossPlatformInputManager.GetAxis("Vertical");
    bool crouch = Input.GetKey(KeyCode.C);

    // calculate move direction to pass to character
    if (m_Cam != null)
    {
    // calculate camera relative direction to move:
    m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
    m_Move = v*m_CamForward + h*m_Cam.right;
    }
    else
    {
    // we use world-relative directions in the case of no main camera
    m_Move = v*Vector3.forward + h*Vector3.right;
    }
    #if !MOBILE_INPUT
    // walk speed multiplier
    if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f;
    #endif

    // pass all parameters to the character control script
    m_Character.Move(m_Move, crouch, m_Jump);
    m_Jump = false;
    }

    void OnCollisionEnter(Collision other)
    {
    if (other.gameObject.CompareTag("Present"))
    {
    Destroy(other.gameObject);
    count = count - 1;
    SetCountText();

    //FindObjectOfType<AudioManager>().Play("CatchingPresent");
    AudioManager.instance.Play("CatchingPresent");
    }
    }

    void SetCountText()
    {
    countText.text = "Missing: " + count.ToString();
    if (count == 0)
    {
    winText.text = "You saved Christmas!";
    }
    }
    }
    }

    编辑 2:Unity 中的层次结构:

    Hierarchy in Unity

    最佳答案

    重新制定我所遵循的方法并通过简单地将音频源添加到 ThirdPersonController(使用我想要调用的 AudioClip)并添加 GetComponent<AudioSource>().Play(); 来解决问题。 if 语句如下:

    void OnCollisionEnter(Collision other)
    {
    if (other.gameObject.CompareTag("Present"))
    {
    Destroy(other.gameObject);
    count = count - 1;
    SetCountText();
    GetComponent<AudioSource>().Play();
    }
    }

    关于c# - 播放器与具有特定标签的对象碰撞时的 Unity3D 播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59285897/

    28 4 0