{"id":2006,"date":"2023-01-07T19:29:34","date_gmt":"2023-01-07T11:29:34","guid":{"rendered":"https:\/\/courtship.top\/?p=2006"},"modified":"2023-01-07T19:29:34","modified_gmt":"2023-01-07T11:29:34","slug":"raycast%e5%b0%84%e7%ba%bf%e6%a3%80%e6%b5%8b","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2023\/01\/07\/raycast%e5%b0%84%e7%ba%bf%e6%a3%80%e6%b5%8b\/","title":{"rendered":"Raycast\u5c04\u7ebf\u68c0\u6d4b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u65b9\u6cd5\u89e3\u6790<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u539f\u51fd\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public static bool Raycast (\nVector3 origin, \/\/\u5c04\u7ebf\u5728\u4e16\u754c\u5750\u6807\u7cfb\u4e2d\u7684\u8d77\u70b9\u3002\nVector3 direction, \/\/\u5c04\u7ebf\u7684\u65b9\u5411\nfloat maxDistance= Mathf.Infinity,  \/\/\u5c04\u7ebf\u5e94\u68c0\u67e5\u78b0\u649e\u7684\u6700\u5927\u8ddd\u79bb\nint layerMask= DefaultRaycastLayers,  \/\/\u53ea\u68c0\u6d4b\u9009\u5b9a\u5c42\u7684\u78b0\u649e\nQueryTriggerInteraction queryTriggerInteraction=QueryTriggerInteraction.UseGlobal\n\/\/\u6307\u5b9a\u8be5\u67e5\u8be2\u662f\u5426\u5e94\u8be5\u547d\u4e2d\u78b0\u649e\u4f53\uff08\u53c2\u6570QueryTriggerInteraction.Ignore\u6307isTrigger\u6253\u5f00\u65f6\u5ffd\u7565\u7269\u4f53\uff09\n);<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3aorigin\u548cdirection\u53ef\u4ee5\u7528Ray\u8fd9\u79cd\u50a8\u5b58\u4e24\u4e2avector\u7684\u53d8\u91cf\uff0cRaycastHit \u7528\u4e8e\u4ece\u5c04\u7ebf\u6295\u5c04\u83b7\u53d6\u4fe1\u606f\u7684\u7ed3\u6784\uff0c\u6240\u4ee5\u4e00\u822c\u7684\u5199\u6cd5\u4e3a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void FixedUpdate()\n    {\n        Ray ray = new Ray(transform.position, transform.forward);\n\n        RaycastHit hitInfo;\n\n        if(Physics.Raycast(ray, out hitInfo,10))\n        {\n            Debug.Log(hitInfo.collider.name);\n            Debug.DrawLine(transform.position,hitInfo.point,Color.green);\n        }\n        else\n        {\n            Debug.DrawLine(ray.origin,ray.origin + ray.direction * 100,Color.red);\n        }\n    }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public static RaycastHit&#91;] RaycastAll (Ray ray, float maxDistance= Mathf.Infinity, int layerMask= DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction= QueryTriggerInteraction.UseGlobal);<\/code><\/pre>\n\n\n\n<p>RaycastAll\u4f1a\u68c0\u6d4b\u6240\u6709\u5c04\u7ebf\u68c0\u6d4b\u5230\u7684\u7ed3\u679c\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u7684\u6570\u7ec4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ray = new Ray(transform.position, transform.forward);\n\n        RaycastHit&#91;] hitsInfo;\n\n        hitsInfo = Physics.RaycastAll(ray, maxSize, layerMask);\n\n        if (hitsInfo.Length != 0)\n        {\n            Debug.DrawLine(ray.origin,ray.origin + ray.direction * 10,Color.red);\n        }\n        else\n        {\n            Debug.DrawLine(ray.origin,ray.origin + ray.direction * 10,Color.green);\n        }\n\n        foreach (var info in hitsInfo)\n        {\n            Debug.Log(info.collider.gameObject.name);\n            info.collider.gameObject.GetComponent&lt;Renderer&gt;().material = highlightM;\n        }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5\u4f5c\u7528<\/h3>\n\n\n\n<p>\u5411\u573a\u666f\u4e2d\u7684\u6240\u6709\u78b0\u649e\u4f53\u6295\u5c04\u4e00\u6761\u5c04\u7ebf\uff0c\u8be5\u5c04\u7ebf\u8d77\u70b9\u4e3a \/origin\/\uff0c\u671d\u5411 \/direction\/\uff0c\u957f\u5ea6\u4e3a \/maxDistance\/\u3002\u60a8\u53ef\u4ee5\u9009\u62e9\u63d0\u4f9b\u4e00\u4e2a LayerMask\uff0c\u4ee5\u8fc7\u6ee4\u6389\u4e0d\u60f3\u751f\u6210\u4e0e\u5176\u78b0\u649e\u7684\u78b0\u649e\u4f53\u3002<br \/>\u60a8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a queryTriggerInteraction \u6765\u63a7\u5236\u662f\u8ba9\u89e6\u53d1\u78b0\u649e\u4f53\u751f\u6210\u547d\u4e2d\u6548\u679c\uff0c\u8fd8\u662f\u4f7f\u7528\u5168\u5c40 Physics.queriesHitTriggers \u8bbe\u7f6e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8e\u5c04\u7ebf\u6295\u5c04\u8d77\u70b9\u4f4d\u4e8e\u78b0\u649e\u4f53\u5185\u7684\u60c5\u51b5\uff0cRaycast \u4e0d\u4f1a\u68c0\u6d4b\u5230\u78b0\u649e\u4f53\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u5c04\u7ebf\u68c0\u6d4b\u5b98\u65b9\u5efa\u8bae\u5728Fixed Update\u4e2d\u4f7f\u7528<\/li>\n\n\n\n<li>\u5c04\u7ebf\u68c0\u6d4b\u7684\u5bf9\u8c61\u5fc5\u987b\u8981\u6709Collider\u7ec4\u4ef6\uff0c\u4e14isTrigger\u7684\u5f00\u5173\u4e0d\u5f71\u54cd\u5c04\u7ebf\u68c0\u6d4b\u7684\u7ed3\u679c\uff08\u53c2\u6570QueryTriggerInteraction\u4e0e\u4e4b\u76f8\u5173\uff09<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"> \u6848\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Camera.<strong>main<\/strong>.ScreenPointToRay(Input.<strong>mousePosition<\/strong>) <\/h3>\n\n\n\n<p>\u5c06\u5c4f\u5e55\u4e0a\u7684\u50cf\u7d20\u5750\u6807\uff08\u5373\u9f20\u6807\u5728\u5c4f\u5e55\u5750\u6807\u4e0a\u7684\u5177\u4f53\u4f4d\u7f6e\uff09\uff0c\u901a\u8fc7ScreenPointToRay()\u65b9\u6cd5\uff0c\u4f20\u5165\u5e76\u8fd4\u56de\u4e00\u6761<strong>\u5728\u4e16\u754c\u5750\u6807\u4e0b\u4ece\u76f8\u673aCamera\u7684\u8fd1\u88c1\u9762\uff0c\u51fa\u53d1\u7a7f\u8fc7\u5c4f\u5e55\u4e0a\u7684\u50cf\u7d20\u70b9\u7684\u5c04\u7ebf<\/strong>\uff0c\u5373\u5c06\u9f20\u6807\u7684\u4f4d\u7f6e\u4fe1\u606f\u4ece\u5c4f\u5e55\u5750\u6807\u7cfb\u8f6c\u6362\u5230Ray\u5c04\u7ebf\u7c7b\u578b<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u9f20\u6807\u4f4d\u7f6e\u5728\u5e73\u9762\u4e0a\u751f\u6210\u7269\u4f53<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public class <strong>PlaceMement <\/strong>: MonoBehaviour\n<strong>{<\/strong><strong>\n<\/strong><strong>    <\/strong>public LayerMask <strong>planemask<\/strong>; \/\/layer\u6807\u7b7e\u7528\u4e8e\u5c04\u7ebf\u53ea\u68c0\u6d4bplane\n    public GameObject <strong>tree<\/strong>;     \/\/\u751f\u6210\u7684\u7269\u4f53\n    private void <strong>FixedUpdate<\/strong>()\n    {\n        Ray ray = Camera.<strong>main<\/strong>.ScreenPointToRay(Input.<strong>mousePosition<\/strong>);\n\n        <em>\/\/<\/em><em>\u8c03\u6574\u5728\u5e73\u9762\u4e0a\u7684\u4f4d\u7f6e\n<\/em><em>        <\/em>if (Physics.<em>Raycast<\/em>(ray, out RaycastHit hitInfo, Mathf.<em>Infinity<\/em>,<strong>planemask<\/strong>))\n        {\n            <strong>tree<\/strong>.<strong>transform<\/strong>.<strong>position <\/strong>= hitInfo.<strong>point<\/strong>;\n            <strong>tree<\/strong>.<strong>transform<\/strong>.<strong>rotation <\/strong>= Quaternion.<em>FromToRotation<\/em>(Vector3.<strong>up<\/strong>, hitInfo.<strong>normal<\/strong>);<em>\/\/<\/em><em>\u4f9d\u9760\u5e73\u9762\u7684\u6cd5\u7ebf\u505a\u53d8\u6362\u89d2\u5ea6\n<\/em><em>        <\/em>}\n\n        if (Input.<em>GetMouseButtonDown<\/em>(0))\n        {\n            <em>Instantiate<\/em>(<strong>tree<\/strong>, hitInfo.<strong>point<\/strong>, Quaternion.<em>FromToRotation<\/em>(Vector3.<strong>up<\/strong>, hitInfo.<strong>normal<\/strong>));\n        }\n    }\n}<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/courtship.top\/wp-content\/uploads\/2023\/01\/\u56fe\u7247.png\" alt=\"\" class=\"wp-image-2025\"\/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">\u79fb\u52a8\u53d1\u5c04\u5149\u675f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using UnityEngine;\n\npublic class <strong>Enemy <\/strong>: MonoBehaviour\n{\n    &#91;SerializeField] private float <strong>moveSpeed<\/strong>;\n    public Transform <strong>target<\/strong>;\n    &#91;SerializeField] private Transform <strong>A<\/strong>, <strong>B<\/strong>;\n\n    public Transform <strong>firePoint<\/strong>;\n    &#91;SerializeField] private float <strong>maxDist<\/strong>;\n    public LayerMask <strong>layer<\/strong>;\n\n    &#91;Header(\"Laser\")] private LineRenderer <strong>lineRender<\/strong>;\n    &#91;SerializeField] private Gradient redColor, <strong>greenColor<\/strong>;\n    private void <strong>Start<\/strong>()\n    {\n        <strong>target <\/strong>= <strong>B<\/strong>;\n        Physics2D.<strong>queriesStartInColliders <\/strong>= false;\n        <strong>lineRender <\/strong>= GetComponentInChildren&lt;LineRenderer>();\n    }\n\n    void <strong>Update<\/strong>()\n    {\n        Move();\n        Detect();\n    }\n\n    void Move()\n    {\n        if (Vector2.<em>Distance<\/em>(<strong>transform<\/strong>.<strong>position<\/strong>, <strong>A<\/strong>.<strong>position<\/strong>) &lt;= 0.1)\n        {\n            <strong>target <\/strong>= <strong>B<\/strong>;\n        }\n\n        if (Vector2.<em>Distance<\/em>(<strong>transform<\/strong>.<strong>position<\/strong>, <strong>B<\/strong>.<strong>position<\/strong>) &lt;= 0.1)\n        {\n            <strong>target <\/strong>= <strong>A<\/strong>;\n        }\n\n        <strong>transform<\/strong>.<strong>position <\/strong>= Vector2.<em>MoveTowards<\/em>(<strong>transform<\/strong>.<strong>position<\/strong>, <strong>target<\/strong>.<strong>position<\/strong>, <strong>moveSpeed <\/strong>* Time.<strong>deltaTime<\/strong>);\n    }\n\n    void Detect()\n    {\n        RaycastHit2D hitInfo;\n        hitInfo = Physics2D.<em>Raycast<\/em>(<strong>firePoint<\/strong>.<strong>position<\/strong>, -<strong>transform<\/strong>.<strong>right <\/strong>* 5, <strong>maxDist<\/strong>,<strong>layer<\/strong>);\n\n        if (hitInfo.<strong>collider <\/strong>!= null)\n        {\n            if (hitInfo.<strong>collider<\/strong>.<strong>tag <\/strong>== \"Block\")\n            {\n                Debug.<em>Log<\/em>(hitInfo.<strong>collider<\/strong>.<strong>gameObject<\/strong>.<strong>name<\/strong>);\n                Debug.<em>DrawLine<\/em>(<strong>firePoint<\/strong>.<strong>position<\/strong>,hitInfo.<strong>point<\/strong>,Color.<strong>red<\/strong>);\n                <strong>lineRender<\/strong>.SetPosition(1,hitInfo.<strong>point<\/strong>);\n                <strong>lineRender<\/strong>.<strong>colorGradient <\/strong>= <strong>greenColor<\/strong>;\n            }\n            <strong>lineRender<\/strong>.SetPosition(0,<strong>firePoint<\/strong>.<strong>position<\/strong>);<em>\/\/linerender<\/em><em>\u521d\u59cb\u70b9\n<\/em><em>        <\/em>}\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\u89e3\u6790 \u539f\u51fd\u6570 \u56e0\u4e3aorigin\u548cdirection\u53ef\u4ee5\u7528Ray\u8fd9\u79cd\u50a8\u5b58\u4e24\u4e2avector\u7684\u53d8\u91cf\uff0cRaycastHit \u7528\u4e8e\u4ece\u5c04\u7ebf &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[13],"tags":[],"class_list":["post-2006","post","type-post","status-publish","format-standard","hentry","category-unity"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=2006"}],"version-history":[{"count":0,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2006\/revisions"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=2006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=2006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=2006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}