{"id":1777,"date":"2022-06-15T00:15:02","date_gmt":"2022-06-14T16:15:02","guid":{"rendered":"https:\/\/104.es\/?p=1777"},"modified":"2022-06-15T00:15:02","modified_gmt":"2022-06-14T16:15:02","slug":"unity-oncollisionenter-application","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2022\/06\/15\/unity-oncollisionenter-application\/","title":{"rendered":"Unity OnCollisionEnter\u7684\u61c9\u7528"},"content":{"rendered":"\n<p>\u5728Unity\u76843D\u4e16\u754c\u4e2d\uff0c\u7576\u4e3b\u89d2\u5728\u4e16\u754c\u4e2d\u8d70\u52d5\u6642\uff0c\u6700\u7c21\u55ae\u7684\u4e92\u52d5\u5c31\u662f\u8b93\u4e3b\u89d2\u53bb\u78b0\u89f8\u5230\u4e16\u754c\u4e2d\u7684\u7269\u9ad4\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u78b0\u649e\u4e8b\u4ef6\u7684\u767c\u751f\uff0c\u4f86\u6c7a\u5b9a\u8981\u7522\u751f\u4ec0\u9ebc\u76f8\u5c0d\u61c9\u7684\u52d5\u4f5c\uff0c\u9019\u4e9b\u52d5\u4f5c\u53ef\u4ee5\u662f\u767c\u51fa\u8072\u97f3\u3001\u64ad\u653e\u5f71\u7247\u6216\u97f3\u6a02\uff0c\u4e5f\u53ef\u4ee5\u8b93\u4e3b\u89d2\u4eba\u7269\u5207\u63db\u5230\u53e6\u5916\u4e00\u500b\u5834\u666f\u53bb\uff0c\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c31\u4f86\u770b\u770b\u8a72\u5982\u4f55\u7de8\u5beb\u9019\u6a23\u7684\u7a0b\u5f0f\u3002<\/p>\n\n\n\n<p>\u5047\u8a2d\u6211\u5011\u5df2\u7d93\u6709\u5e7e\u500b3D\u7684\u4e16\u754c\uff0c\u5206\u5225\u653e\u5728\u4e0d\u540c\u7684Scene\u4e2d\uff0c\u5982\u540c\u524d\u9762\u6587\u7ae0\u4e2d\u7684\u8aaa\u660e\uff0c\u8981\u80fd\u5920\u5207\u63db\u5834\u666f\uff0c\u9700\u8981\u5728Build Settings\u52a0\u5165\u5b83\u5011\uff0c\u9019\u9ede\u975e\u5e38\u5730\u91cd\u8981\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"635\" height=\"600\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2022\/06\/image-38.png?w=635\" alt=\"\" class=\"wp-image-1781\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2022\/06\/image-38.png 635w, https:\/\/104.es\/wp-content\/uploads\/2022\/06\/image-38-300x283.png 300w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/figure>\n\n\n\n<p>\u63a5\u8457\uff0c\u5047\u8a2d\u6211\u5011\u5728\u4e16\u754c\u4e2d\u8a2d\u8a08\u4e86\u5e7e\u500bCube\u4ee5\u53caSphere\u7269\u4ef6\uff0c\u653e\u5728\u4e3b\u89d2\u524d\u9762\u7b49\u5f85\u4e3b\u89d2\u7684\u4e92\u52d5\uff0c\u5982\u4e0b\u5716\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1102\" height=\"561\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2022\/06\/image-39.png?w=1024\" alt=\"\" class=\"wp-image-1783\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2022\/06\/image-39.png 1102w, https:\/\/104.es\/wp-content\/uploads\/2022\/06\/image-39-300x153.png 300w, https:\/\/104.es\/wp-content\/uploads\/2022\/06\/image-39-1024x521.png 1024w, https:\/\/104.es\/wp-content\/uploads\/2022\/06\/image-39-768x391.png 768w\" sizes=\"auto, (max-width: 1102px) 100vw, 1102px\" \/><\/figure>\n\n\n\n<p>\u5176\u4e2d\u90a3\u500b\u7403\u5e0c\u671b\u5b83\u53ef\u4ee5\u81ea\u884c\u5f48\u8df3\uff0c\u4e26\u5728\u63a5\u89f8\u5730\u9762\u6642\u767c\u51fa\u97f3\u6548\uff0c\u7b2c\u4e00\u4ef6\u4e8b\u662f\u628a\u97f3\u6548\u62d6\u66f3\u5230\u7403\u7269\u4ef6\u4e0a\u518d\u653e\u958b\uff0c\u7136\u5f8c\u8a2d\u8a08\u4ee5\u4e0b\u7684\u7a0b\u5f0f\uff0c\u8b93\u5b83\u53ef\u4ee5\u81ea\u884c\u767c\u51fa\u97f3\u6548\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\npublic class BallSound : MonoBehaviour\n{\n    public AudioSource BounceSFX = null;\n\n    private void OnCollisionEnter(Collision collision)\n    {\n        BounceSFX.Play();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u7576\u7136\uff0c\u524d\u63d0\u662f\u5b83\u5fc5\u9808\u8981\u6709\u4e00\u500b\u53ef\u4ee5\u8b93\u5b83\u5f48\u8df3\u7684Physic Meterial\uff0c\u4e26\u8a2d\u5b9a\u597d\u6b63\u78ba\u7684AudioSource\u3002\u53e6\u59163\u500bCube\u7269\u4ef6\u4e00\u500b\u7528\u4f86\u5207\u63db\u5834\u666f\uff0c\u4e00\u500b\u7528\u4f86\u64ad\u653e\u5f71\u7247\uff0c\u53e6\u5916\u4e00\u500b\u5247\u662f\u7528\u4f86\u64ad\u653e\u8072\u97f3\uff0c\u5b83\u7684\u7a0b\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\nusing UnityEngine.SceneManagement;\nusing UnityEngine.Video;\n\npublic class PlayerControl : MonoBehaviour\n{\n    private bool VideoPlaying = false;\n    private void OnCollisionEnter(Collision collision)\n    {\n        if (collision.gameObject.name.Contains(\"CubeSound\"))\n        {\n            if (collision.gameObject.GetComponent&lt;AudioSource&gt;() != null)\n            {\n                collision.gameObject.GetComponent&lt;AudioSource&gt;().Play();\n            }\n        }\n        else if (collision.gameObject.name.Contains(\"CubeTV\"))\n        {\n            if (collision.gameObject.GetComponent&lt;VideoPlayer&gt;() != null)\n            {\n                if (VideoPlaying)\n                {\n                    collision.gameObject.GetComponent&lt;VideoPlayer&gt;().Pause();\n                    VideoPlaying = false;\n                }\n                else\n                {\n                    VideoPlaying = true;\n                    collision.gameObject.GetComponent&lt;VideoPlayer&gt;().Play();\n                }\n                \n            }\n        }\n        else if (collision.gameObject.GetComponent&lt;SceneInfo&gt;()!=null)\n        {\n            SceneManager.LoadScene(collision.gameObject.GetComponent&lt;SceneInfo&gt;().SceneName);\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u9019\u500b\u7a0b\u5f0f\u6211\u5011\u628a\u5b83\u547d\u540d\u70baPlayerControl\uff0c\u4e26\u628a\u5b83\u9644\u52a0\u5728\u4e3b\u89d2\u8eab\u4e0a\u3002\u53e6\u5916\uff0c\u90a3\u500b\u5207\u63db\u5834\u666f\u7684\u64cd\u4f5c\uff0c\u56e0\u70ba\u9084\u9700\u8981\u6709\u4e00\u500b\u8981\u5207\u63db\u5230\u7684\u5834\u666f\u540d\u7a31\uff0c\u6240\u4ee5\u8a72\u7269\u9ad4\u4e5f\u9700\u8981\u518d\u7de8\u5beb\u53e6\u5916\u4e00\u500b\u7a0b\u5f0f\uff0c\u5728\u6b64\u628a\u5b83\u547d\u540d\u70baSceneInfo\uff0c\u5167\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\npublic class SceneInfo : MonoBehaviour\n{\n    public string SceneName = null;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Unity\u76843D\u4e16\u754c\u4e2d\uff0c\u7576\u4e3b\u89d2\u5728\u4e16\u754c\u4e2d\u8d70\u52d5\u6642\uff0c\u6700\u7c21\u55ae\u7684\u4e92\u52d5\u5c31\u662f\u8b93\u4e3b\u89d2\u53bb\u78b0\u89f8\u5230\u4e16\u754c\u4e2d\u7684\u7269\u9ad4\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u78b0\u649e\u4e8b\u4ef6\u7684\u767c\u751f\uff0c\u4f86\u6c7a\u5b9a\u8981\u7522\u751f\u4ec0\u9ebc\u76f8\u5c0d\u61c9\u7684\u52d5\u4f5c\uff0c\u9019\u4e9b\u52d5\u4f5c\u53ef\u4ee5\u662f\u767c\u51fa\u8072\u97f3\u3001\u64ad\u653e\u5f71\u7247\u6216\u97f3\u6a02\uff0c\u4e5f\u53ef\u4ee5\u8b93\u4e3b\u89d2\u4eba\u7269\u5207\u63db\u5230\u53e6\u5916\u4e00\u500b\u5834\u666f\u53bb\uff0c\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c31\u4f86\u770b\u770b\u8a72\u5982\u4f55\u7de8\u5beb\u9019\u6a23\u7684\u7a0b\u5f0f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[18],"tags":[],"class_list":["post-1777","post","type-post","status-publish","format-standard","hentry","category-unity"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/1777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=1777"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/1777\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=1777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=1777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=1777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}