- Home /
mouse input to touch input help please
#pragma strict
var prefab : Transform;
var dist : float = 10.0;
var curr : Transform;
function Update() {
if (Input.GetMouseButtonDown(0)) {
var pos = Input.mousePosition;
pos.z = dist;
pos = Camera.main.ScreenToWorldPoint(pos);
curr = Instantiate(prefab, pos, Quaternion.identity);
}
if (Input.GetMouseButton(0)) {
pos = Input.mousePosition;
pos.z = dist;
curr.position = Camera.main.ScreenToWorldPoint(pos);
}
}
hello, i am trying to convert this code to touch input, however when i test on a mobile device, nothing happens with the swipe. i've tried the following :
var prefab : Transform;
var dist : float = 10.0;
var curr : Transform;
function Update() {
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began) {
var pos : Vector3 = Input.GetTouch(0).position;
pos.z = dist;
pos = Camera.main.ScreenToWorldPoint(pos);
curr = Instantiate(prefab, pos, Quaternion.identity);
}
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved) {
pos = pos;
pos.z = dist;
curr.position = Camera.main.ScreenToWorldPoint(pos);
}
}
also i have the following script on the prefab which should be spawned:
var speed : float = 0.1;
var trail : GameObject;
function Update () {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) {
Destroy(trail);
}
if (Input.touchCount > 0 &&
Input.GetTouch(0).phase == TouchPhase.Moved) {
var touchposition:Vector2 = Input.GetTouch(0).position;
transform.Translate (touchposition.x * speed,
touchposition.y * speed, 0);
}
}
i have tried tweaking the code a bit, but nothing seems to work. :( please assist?
Comment
Your answer
Follow this Question
Related Questions
Detect if finger lifted off screen 1 Answer
How do you force points into a straight a line using swipe? 0 Answers
Swipe controls error "Cannot implicitly convert type 'UnityEngine.Touch' to 'Touch' " 1 Answer
Different resolutions make the swipe of my player go at different speeds on different devices 0 Answers