{"id":210,"date":"2015-02-13T19:06:12","date_gmt":"2015-02-13T11:06:12","guid":{"rendered":"http:\/\/qxu1606620110.my3w.com\/?p=210"},"modified":"2016-10-11T09:36:15","modified_gmt":"2016-10-11T01:36:15","slug":"unity%e5%b0%84%e7%ba%bf-%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=210","title":{"rendered":"Unity\u5c04\u7ebf_\u7b14\u8bb0"},"content":{"rendered":"<p> \/\/\u753b\u51fa\u5f53\u524d\u5c04\u7ebf <\/p>\n<pre class=\"lang:c# decode:true \" title=\"\u753b\u51fa\u5f53\u524d\u5c04\u7ebf\" >Debug.DrawRay(tempVec,transform.forward, Color.green, 2f);<\/pre>\n<p>\u901a\u8fc7\u4e8c\u8fdb\u5236\u8f6c\u6362\u9009\u4e2d\u5c42     \uff08\u5c04\u7ebf\uff0c\u5c04\u7ebf\u78b0\u5230\u7684\u7269\u4f53\u4fe1\u606f\uff0c\u8ddd\u79bb\uff0c\u5c42\uff09<\/p>\n<pre class=\"lang:c# decode:true \" title=\"\u901a\u8fc7\u4e8c\u8fdb\u5236\u8f6c\u6362\u9009\u4e2d\u5c42\" >\/\/\u5b9a\u4e49\u5c04\u7ebf\u63a5\u6536\u9f20\u6807\u7684\u5750\u6807\r\nRay r = Camera.main.ScreenPointToRay (Input.mousePosition);\r\nif (Input.GetMouseButtonDown (0))\r\n{\r\n\tRaycastHit rc;\r\n\tint cubeIndex = LayerMask.NameToLayer (\"Cube\");\r\n\tint sphereIndex = LayerMask.NameToLayer (\"Sphere\");\r\n\t\/\/r\u4e3a\u9f20\u6807\u4f4d\u7f6e\u548c\u65b9\u5411  rc\u4e3a\u5b58\u50a8\u6295\u5c04\u5230\u7684\u7269\u4f53\u4fe1\u606f, 100f\u4e3a\u6295\u5c04\u8ddd\u79bb, 1&lt;&lt;layerIndex \u4e3acube\u6240\u5728\u5c42\r\n\t\/\/1&lt;&lt;layerIndex \u4e3a\u5c061\u5411\u524d\u63a8layerIndex\u4f4d\uff0c\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\r\n\t\/\/1 &lt;&lt; cubeIndex | 1 &lt;&lt; sphereIndex) \u5c06cubeIndex\u548csphereIndex\u540c\u65f6\u88651\uff0c\u8f6c\u4e3a\u4e8c\u8fdb\u5236\r\n\tif (Physics.Raycast (r,out rc, 100f, 1 &lt;&lt; cubeIndex | 1 &lt;&lt; sphereIndex))    \r\n\t{\r\n\t\tDestroy (rc.transform.gameObject);\r\n\t}\r\n}<\/pre>\n<p>\u901a\u8fc7\u5916\u754c\u5c42\u63a9\u7801\u53d8\u91cf\u9009\u4e2d\u5c42     \uff08\u5c04\u7ebf\uff0c\u5c04\u7ebf\u78b0\u5230\u7684\u7269\u4f53\u4fe1\u606f\uff0c\u8ddd\u79bb\uff0c\u5c42\uff09<\/p>\n<pre class=\"lang:c# decode:true \" title=\"\u901a\u8fc7\u5916\u754c\u5c42\u63a9\u7801\u53d8\u91cf\u9009\u4e2d\u5c42\" >\/\/\u5b9a\u4e49\u5c42\u63a9\u7801\u516c\u5171\u53d8\u91cf \u7528\u4e8e\u63a5\u6536\u9009\u4e2d\u4e3a\u54ea\u51e0\u4e2a\u5c42\r\npublic LayerMask lm;\r\n\/\/ Update is called once per frame\r\nvoid Update () {\r\n\t\/\/\u5b9a\u4e49\u5c04\u7ebf\u63a5\u6536\u9f20\u6807\u7684\u5750\u6807\r\n\tRay r = Camera.main.ScreenPointToRay (Input.mousePosition);\r\n\tif (Input.GetMouseButtonDown (0))\r\n\t{\r\n\t\tRaycastHit rc;\r\n\t\tint cubeIndex = LayerMask.NameToLayer (\"Cube\");\r\n\t\tint sphereIndex = LayerMask.NameToLayer (\"Sphere\");\r\n\t\t\/\/r\u4e3a\u9f20\u6807\u4f4d\u7f6e\u548c\u65b9\u5411  rc\u4e3a\u5b58\u50a8\u6295\u5c04\u5230\u7684\u7269\u4f53\u4fe1\u606f, 100f\u4e3a\u6295\u5c04\u8ddd\u79bb, lm\u9009\u4e2d\u7684\u5c42\r\n\t\tif (Physics.Raycast (r,out rc, 100f, lm))    \r\n\t\t{\r\n\t\t\tDestroy (rc.transform.gameObject);\r\n\t\t\trc.point;     \/\/\u83b7\u53d6rc\u5750\u6807\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<p>\u4f7f\u7269\u4f53\u8ddf\u968f\u9f20\u6807\u70b9\u51fb\u4f4d\u7f6e\u884c\u9a76<\/p>\n<pre class=\"lang:c# decode:true \" title=\"\u4f7f\u7269\u4f53\u8ddf\u968f\u9f20\u6807\u70b9\u51fb\u4f4d\u7f6e\u884c\u9a76\" >GameObject g;\r\nbool isMove;\r\nbool isMoveOver;\r\nVector3 vec;\r\nVector3 vec2;\r\n\/\/ Use this for initialization\r\nvoid Start () {\r\n\tg = GameObject.Find (\"Player\");\r\n\tisMove = false;\r\n}\r\n\/\/ Update is called once per frame\r\nvoid Update () {\r\n\tMoveTo ();\r\n\tisMoving ();\r\n}\r\n\/\/\u9f20\u6807\u70b9\u51fb\uff0c\u5e76\u5224\u65ad\u70b9\u51fb\u4f4d\u7f6e\u662f\u5426\u4e3a\u5730\u9762\r\nvoid MoveTo ()\r\n{\r\n\tif (Input.GetMouseButton (0))\r\n\t{\r\n\t\tRay r = Camera.main.ScreenPointToRay (Input.mousePosition);\r\n\t\tRaycastHit rc = new RaycastHit ();\r\n\t\tif (Physics.Raycast (r,out rc))\r\n\t\t{\r\n\t\t\tif (rc.transform.gameObject.tag == \"plane\")\r\n\t\t\t{\r\n\t\t\t\tisMove = true;\r\n\t\t\t\tvec = rc.point;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\/\/\u901a\u8fc7Mathf.Lerp\u6765\u53d8\u901f\u79fb\u52a8\r\nvoid isMoving ()\r\n{\r\n\tif (isMove)\r\n\t{\r\n\t\tfloat x=Mathf.Lerp(g.transform.position.x,vec.x,Time.deltaTime * 2);\r\n\t\tfloat z=Mathf.Lerp(g.transform.position.z,vec.z,Time.deltaTime * 2);\r\n\t\tg.transform.position =  new Vector3 (x,g.transform.position.y,z);\r\n\t\t\/\/            isMoveOver = true;\r\n\t\tif (g.transform.position == vec)\r\n\t\t{\r\n\t\t\tisMove = false;\r\n\t\t}\r\n\t}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/\u753b\u51fa\u5f53\u524d\u5c04\u7ebf Debug.DrawRay(tempVec,transform &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=210\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/210"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=210"}],"version-history":[{"count":3,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":773,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions\/773"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}