Hello i am beginner in unity! I read many questions and answers and I could not find right answer for this. I want to emit stars on the touched position in mobile.
What I tried for instantiating the particle is like this :
public class StarEmit : MonoBehaviour {
private Touch touch;
void Start () {
touch = Input.GetTouch(0);
}
void Update () {
float x = touch.position.x/ Screen.width;
float y = touch.position.y/ Screen.height;
transform.position = new Vector3(x,y,0);
}
}
You should get touch position inside Update()
method and not in Start()
as Start()
is executed only once and not per frame. So your code should be:
public class StarEmit : MonoBehaviour {
Touch touch;
void Update () {
if(Input.touchCount > 0)
{
touch = Input.GetTouch(0);
}
float x = touch.position.x/ Screen.width;
float y = touch.position.y/ Screen.height;
transform.position = new Vector3(x,y,0);
}
}
The code above will teleport the transform of gameobject to touch position. If you want to smoothly move the transform then you should consider using Vector3.Lerp.