- Home /
Touch position to fit in screen
Hi, I want to paint on touch screen with prefab, where I touch there will be a red sprite (prefab).
Here is my code :
var i : Vector2;
var prefab : Transform;
function FixedUpdate () {
i = Input.GetTouch(0).position;
var touch : UnityEngine.Touch;
if(Input.touchCount >0)
{
Instantiate (prefab, i ,transform.rotation);
Debug.Log(i);
}
}
Here is screenshot when I click on 4 corners
The White thing is ortographic Camera.
screenshot.png
(207.8 kB)
Comment
Best Answer
Answer by 767_2 · Sep 27, 2014 at 11:47 AM
function Update ()
{
if (Input.touchCount > 0)
{
var ray =Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
Instantiate (prefab, hit.transform ,Quaternion.identity);
}
}
}