{"id":1886,"date":"2018-03-02T05:14:08","date_gmt":"2018-03-02T05:14:08","guid":{"rendered":"http:\/\/www.incredigeek.com\/home\/?p=1886"},"modified":"2018-03-02T05:15:22","modified_gmt":"2018-03-02T05:15:22","slug":"unity-2d-c-script-to-spawn-object-on-screen-at-touch-position","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/unity-2d-c-script-to-spawn-object-on-screen-at-touch-position\/","title":{"rendered":"Unity 2D C# Script to Spawn Object on Screen at Touch Position"},"content":{"rendered":"<p>You&#8217;ll need a touch screen to test this out.\u00a0 Easiest way is to use the Unity Remote 5 app.\u00a0 It allows you to use your Android device as a touch screen input.<\/p>\n<p>Start by creating a new empty C# script named &#8220;TouchScript&#8221; and paste in the following code.<\/p>\n<pre><code>using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\npublic class TouchScript : MonoBehaviour {\r\n\r\npublic GameObject bullet;\r\n\r\nvoid Start () {\r\n\r\n}\r\n\r\nvoid Update () {\r\nTouch myTouch = Input.GetTouch (0);\r\nfor(int i =0; i &lt; Input.touchCount; i++)\r\nif (myTouch.phase == TouchPhase.Began) {\r\n{\r\nDebug.Log (\"Touch Position\" + myTouch.position);\r\nSpawnBullet (myTouch);\r\n}\r\n}\r\n}\r\n\r\nvoid SpawnBullet(Touch fireTouch) {\r\nVector3 touchPos = Camera.main.ScreenToWorldPoint (fireTouch.position);\r\ntouchPos.z = 1; \/\/ Puts the z coordinates at 1 so it is visible to the camera.\r\nDebug.Log(\"Vector3 Pos\" + touchPos);\r\nInstantiate (bullet, touchPos, Quaternion.identity);\r\n}\r\n}<\/code><\/pre>\n<p>Now you&#8217;ll need to put your script on a game object.\u00a0 You should be able to drag and drop it on any game object, in this case the Main Camera &#8220;green&#8221;.<\/p>\n<p>Next drag and drop the game object prefab to the script &#8220;red&#8221;.\u00a0 This tells the script which game object to spawn or instantiate when you touch the screen.<\/p>\n<p><a href=\"http:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1889\" src=\"http:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript2-1024x555.png\" alt=\"\" width=\"584\" height=\"317\" srcset=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript2-1024x555.png 1024w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript2-300x163.png 300w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript2-768x416.png 768w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript2-500x271.png 500w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript2.png 1920w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><\/p>\n<p>Run the scene.\u00a0 It should now create a game object where you touch on the screen.<\/p>\n<p><a href=\"http:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1891\" src=\"http:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript3-1024x555.png\" alt=\"\" width=\"584\" height=\"317\" srcset=\"https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript3-1024x555.png 1024w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript3-300x163.png 300w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript3-768x416.png 768w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript3-500x271.png 500w, https:\/\/www.incredigeek.com\/home\/wp-content\/uploads\/2018\/03\/TouchScript3.png 1920w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ll need a touch screen to test this out.\u00a0 Easiest way is to use the Unity Remote 5 app.\u00a0 It allows you to use your Android device as a touch screen input. Start by creating a new empty C# script &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/unity-2d-c-script-to-spawn-object-on-screen-at-touch-position\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[325],"tags":[82,403,404,405,406,326,327],"class_list":["post-1886","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-c","tag-development","tag-game-dev","tag-scripting","tag-touch","tag-unity","tag-unity3d"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/1886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=1886"}],"version-history":[{"count":11,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/1886\/revisions"}],"predecessor-version":[{"id":1899,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/1886\/revisions\/1899"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=1886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=1886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=1886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}