碰撞声

时间:2016-01-03 19:13:00

标签: c# audio unity3d game-engine

可以请某人为我格式化代码吗?

我的代码:

using UnityEngine;
using System.Collections;

public class ScorePoint : MonoBehaviour 
{

    private AudioSource audioSource;
    public AudioClip Scored;

    void OnTriggerEnter2D(Collider2D collider) 
    {

        if(collider.tag == "Player") 
        {
            audioSource = GetComponent<AudioSource>();
            audioSource.clip = Scored;
            audioSource.Play();
        }
    }
}

此代码仅在对象触发时才有效。

1 个答案:

答案 0 :(得分:0)

如果您想使用未设置为触发器的对撞机,则应使用OnCollisionEnter2d代替。请务必将参数从Collider2d更改为Collision2d

using UnityEngine;
using System.Collections;

public class ScorePoint : MonoBehaviour
{
    private AudioSource audioSource;
    public AudioClip Scored;

    void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            audioSource = GetComponent<AudioSource>();
            audioSource.clip = Scored;
            audioSource.Play();
        }
    }
}

<强> OnTriggerEnter2d

  

当另一个对象进入连接到此对象的触发器对撞机时发送(仅限2D物理)。

http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html

<强> OnCollisionEnter2d

  

当传入的对撞机与此物体的对撞机接触时发送(仅限2D物理)。

http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html