{"id":1527,"date":"2022-05-24T19:26:59","date_gmt":"2022-05-24T11:26:59","guid":{"rendered":"https:\/\/104.es\/?p=1527"},"modified":"2022-05-24T19:26:59","modified_gmt":"2022-05-24T11:26:59","slug":"unity-on-collision-change-color","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2022\/05\/24\/unity-on-collision-change-color\/","title":{"rendered":"Unity\u78b0\u649e\u4e4b\u5f8c\u96a8\u6a5f\u8b8a\u8272\u7a0b\u5f0f"},"content":{"rendered":"\n<p>Unity\u7684\u7269\u4ef6\u53ef\u4ee5\u52a0\u4e0aRigidbody\u8b93\u7269\u9ad4\u5177\u6709\u525b\u6027\uff0c\u4e26\u53ef\u4ee5\u8b93\u8a72\u7269\u9ad4\u5c0e\u5165\u7269\u7406\u7279\u6027\u3002\u6709\u4e86\u7269\u7406\u7279\u6027\u4e4b\u5f8c\uff0c\u5c31\u53ef\u4ee5\u8b93\u7269\u9ad4\u4e4b\u9593\u5177\u6709\u4e00\u4e9b\u5305\u62ec\u78b0\u649e\u5728\u5167\u7684\u884c\u70ba\u8868\u73fe\u3002\u5728\u78b0\u649e\u7684\u904e\u7a0b\u4e2d\uff0c\u6709\u4e9b\u7269\u9ad4\u662f\u53ef\u4ee5\u8868\u73fe\u51fa\u5177\u6709\u5f48\u6027\u7684\u884c\u70ba\u73fe\u8c61\uff0c\u4ee5\u5713\u7403\u7269\u9ad4\u70ba\u4f8b\uff0c\u6211\u5011\u628a\u5b83\u52a0\u4e0aRigidbody\uff0c\u7136\u5f8c\u518d\u52a0\u4e0a\u4e00\u500b\u547d\u540d\u70baBounce\u7684\u7269\u7406\u6750\u6599\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"503\" height=\"164\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2022\/05\/image-12.png?w=503\" alt=\"\" class=\"wp-image-1530\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2022\/05\/image-12.png 503w, https:\/\/104.es\/wp-content\/uploads\/2022\/05\/image-12-300x98.png 300w\" sizes=\"auto, (max-width: 503px) 100vw, 503px\" \/><\/figure>\n\n\n\n<p>\u4e0a\u8ff0\u7684\u7269\u7406\u6750\u6599\u628a\u5b83\u9644\u52a0\u5230\u8a72\u7403\u9ad4\u7684Collider\u4e2d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"504\" height=\"142\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2022\/05\/image-13.png?w=504\" alt=\"\" class=\"wp-image-1532\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2022\/05\/image-13.png 504w, https:\/\/104.es\/wp-content\/uploads\/2022\/05\/image-13-300x85.png 300w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><\/figure>\n\n\n\n<p>\u63a5\u8457\u8acb\u5beb\u5165\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u78bc\uff0c\u5728\u6b64\u6211\u5011\u628a\u5b83\u547d\u540d\u70baOnHitChangeColor\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\npublic class OnHitChangeColor : MonoBehaviour\n{\n    private void OnCollisionEnter(Collision collision)\n    {\n        float r = UnityEngine.Random.Range(0f, 1f);\n        float g = UnityEngine.Random.Range(0f, 1f);\n        float b = UnityEngine.Random.Range(0f, 1f);\n        Color new_color = new Color(r, g, b);\n        GetComponent&lt;Renderer&gt;().material.color = new_color;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u6700\u5f8c\u628a\u9019\u6bb5\u7a0b\u5f0f\u78bc\u9644\u52a0\u5230\u7403\u9ad4\u4e0a\uff0c\u518d\u6b21\u57f7\u884cUnity\u61c9\u7528\u7a0b\u5f0f\uff0c\u5373\u53ef\u770b\u5230\u7403\u9ad4\u6bcf\u6b21\u5f48\u8df3\u6642\uff0c\u90fd\u6703\u6539\u8b8a\u81ea\u5df1\u7684\u984f\u8272\u3002\u57f7\u884c\u7d50\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Unity\u8b93\u7403\u5f48\u8df3\u4e26\u8b8a\u8272\u7684\u65b9\u6cd5\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/kLq_WR-1EEA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>nity\u7684\u7269\u4ef6\u53ef\u4ee5\u52a0\u4e0aRigidbody\u8b93\u7269\u9ad4\u5177\u6709\u525b\u6027\uff0c\u4e26\u53ef\u4ee5\u8b93\u8a72\u7269\u9ad4\u5c0e\u5165\u7269\u7406\u7279\u6027\u3002\u6709\u4e86\u7269\u7406\u7279\u6027\u4e4b\u5f8c\uff0c\u5c31\u53ef\u4ee5\u8b93\u7269\u9ad4\u4e4b\u9593\u5177\u6709\u4e00\u4e9b\u5305\u62ec\u78b0\u649e\u5728\u5167\u7684\u884c\u70ba\u8868\u73fe\u3002\u5728\u78b0\u649e\u7684\u904e\u7a0b\u4e2d\uff0c\u6709\u4e9b\u7269\u9ad4\u662f\u53ef\u4ee5\u8868\u73fe\u51fa\u5177\u6709\u5f48\u6027\u7684\u884c\u70ba\u73fe\u8c61\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u5728\u78b0\u649e\u6642\u57f7\u884c\u7a0b\u5f0f\u4ee5\u6539\u8b8a\u4e00\u4e9b\u7269\u4ef6\u7684\u5916\u89c0\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-1527","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\/1527","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=1527"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/1527\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=1527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=1527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=1527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}