{"id":536,"date":"2026-05-06T22:14:41","date_gmt":"2026-05-06T22:14:41","guid":{"rendered":"https:\/\/sites.wsagames.com\/jz15n24\/?p=536"},"modified":"2026-05-13T00:05:11","modified_gmt":"2026-05-13T00:05:11","slug":"another-option-more-flexible-raycast","status":"publish","type":"post","link":"https:\/\/sites.wsagames.com\/jz15n24\/2026\/05\/06\/another-option-more-flexible-raycast\/","title":{"rendered":"Another Option: More Flexible Raycast"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When creating footstep sound changes for different ground materials, I also researched another common method: using Raycast to detect the ground material underneath the player.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Raycast can be understood as an \u201cinvisible ray\u201d in Unity. If it is used in a footstep system, a ray can be cast downward from the player\u2019s feet to detect the collider, tag, layer, or material that the player is currently standing on. Then, based on the detection result, the GroundMaterial Switch in Wwise can be changed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unity\u2019s official documentation also explains that Physics.Raycast casts a ray against colliders in the scene and returns information about the object it hits.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I tried writing some code for this. Although I have not fully tested the whole system yet, it would roughly look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1009\" height=\"608\" src=\"https:\/\/sites.wsagames.com\/jz15n24\/wp-content\/uploads\/sites\/70\/2026\/05\/image-21.png\" alt=\"\" class=\"wp-image-540\" srcset=\"https:\/\/sites.wsagames.com\/jz15n24\/wp-content\/uploads\/sites\/70\/2026\/05\/image-21.png 1009w, https:\/\/sites.wsagames.com\/jz15n24\/wp-content\/uploads\/sites\/70\/2026\/05\/image-21-300x181.png 300w, https:\/\/sites.wsagames.com\/jz15n24\/wp-content\/uploads\/sites\/70\/2026\/05\/image-21-768x463.png 768w\" sizes=\"(max-width: 1009px) 100vw, 1009px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The advantage of this method is that it is more automated. As long as the ground colliders and tags in the scene are set clearly, the script can automatically detect the type of ground under the player wherever they walk. It is suitable for more complex scenes, such as areas that contain multiple materials like grass, wooden bridges, stone paths, mud, and water, especially when the boundaries between these areas are fragmented and inconvenient to cover manually with detectors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, Raycast also has higher requirements for the scene setup. First, the ground must have a collider; otherwise, the ray cannot detect it. Second, each type of ground needs clear tag, layer, or material information, otherwise the script cannot determine which footstep sound the detected surface should correspond to. At material boundaries, Raycast may also switch frequently between two materials because of small changes in the detection point. The origin and distance of the ray also need to be adjusted carefully: if the ray is too short, it may not hit the ground; if it is too long, it may pass through the current ground and hit another collider underneath.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In comparison, I eventually chose the transparent trigger detector method for this implementation. This is because we only have three ground materials: grass, dirt, and wooden bridge. Also, the shapes of the dirt path and wooden bridge are very regular, so I can simply place trigger detectors over the wooden bridge and dirt path areas. The logic is to play one type of footstep sound inside the collider range, and another type of footstep sound outside that range. This method is less likely to affect the existing footstep system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the future, if the project includes more complex ground types, such as mud, stone paths, water surfaces, or indoor floors, raycast-based surface detection would be a direction worth expanding further.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unity\u2019s trigger collider can call `OnTriggerEnter` when the player enters a trigger area. The official documentation also explains that `OnTriggerEnter` is called when a collider enters a trigger. This approach is more suitable for the current project, because I only need to switch footsteps between the bridge and grass, and I do not want to heavily rewrite the existing `FootstepPlayer` system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Resources\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/MonoBehaviour.OnTriggerEnter.html\">https:\/\/docs.unity3d.com\/ScriptReference\/MonoBehaviour.OnTriggerEnter.html<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.unity3d.com\/6000.4\/Documentation\/ScriptReference\/Physics.Raycast.html\">https:\/\/docs.unity3d.com\/6000.4\/Documentation\/ScriptReference\/Physics.Raycast.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When creating footstep sound changes for different ground materials, I also researched another common method: using Raycast to detect the ground material underneath the player. Raycast can be understood as an \u201cinvisible ray\u201d in Unity. If it is used in a footstep system, a ray can be cast downward from the player\u2019s feet to detect [&hellip;]<\/p>\n","protected":false},"author":61,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-536","post","type-post","status-publish","format-standard","hentry","category-weekly-development"],"_links":{"self":[{"href":"https:\/\/sites.wsagames.com\/jz15n24\/wp-json\/wp\/v2\/posts\/536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.wsagames.com\/jz15n24\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.wsagames.com\/jz15n24\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.wsagames.com\/jz15n24\/wp-json\/wp\/v2\/users\/61"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.wsagames.com\/jz15n24\/wp-json\/wp\/v2\/comments?post=536"}],"version-history":[{"count":1,"href":"https:\/\/sites.wsagames.com\/jz15n24\/wp-json\/wp\/v2\/posts\/536\/revisions"}],"predecessor-version":[{"id":541,"href":"https:\/\/sites.wsagames.com\/jz15n24\/wp-json\/wp\/v2\/posts\/536\/revisions\/541"}],"wp:attachment":[{"href":"https:\/\/sites.wsagames.com\/jz15n24\/wp-json\/wp\/v2\/media?parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.wsagames.com\/jz15n24\/wp-json\/wp\/v2\/categories?post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.wsagames.com\/jz15n24\/wp-json\/wp\/v2\/tags?post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}