效果视频:https://weibo.com/tv/v/ICwjRxY6U?fid=1034:4491806835146754
场景里只有一个圆球和一个平面。
圆球的名字叫做redball。


圆球加入Rigibody,使其有重力可以下落。

在平面上加入Audio Source,取消掉Play on awake的勾选

新建一个C#脚本,拖到平面上,运行即可。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test1 : MonoBehaviour
{
AudioSource audio;
// Start is called before the first frame update
void Start()
{
audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "redball")
{
gameObject.GetComponent<Renderer>().material.color = Color.red;
audio.Play();
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.name == "redball")
{
gameObject.GetComponent<Renderer>().material.color = Color.white;
}
}
}
网友评论