Question by
MG-BS-500 · Mar 17, 2016 at 04:28 PM ·
touchtransform.positiontouchphaseguibuttonfinger
Transform Position - GUI Button
Hi, i have this script:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public GameObject Player;
private Vector2 touchPos;
private bool show = false;
void Start() {
GUI.enabled = false;
}
void Update() {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
// SHOW BUTTON
GUI.enabled = true;
show = !show;
}
}
void OnGUI ()
{
if (Input.touchCount > 0) {
//TOUCH POSITION
Touch touch;
touch = Input.GetTouch (0);
touchPos.x = touch.position.x;
touchPos.y = Screen.height - touch.position.y;
}
if (show) {
// GUI BUTTON POSITION = TOUCH POSITION
if (GUI.Button (new Rect (touchPos.x + 30, touchPos.y + 30, 300, 100), "Teleport")) {
// TRANSFORM POSITION - HIDE BUTTON
Player.transform.position = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
GUI.enabled = false;
show = !show;
}
}
}
}
With the first touch I decide the position of the button, after finishing the phase of touch the button remains open. With the second touch on the button I decide the position of the player but I have a problem: When I use the touch on the button should be two fingers on the screen otherwise the player position is not changed, how can I make the button work with just one finger ?
Thanks in advance.
Comment
Your answer
Follow this Question
Related Questions
2D touch - Why is this movement so jittery? 1 Answer
How do I make a rigid body move towards position of touch 0 Answers
iOS moving up and down question ( code problem ) 0 Answers
Enhanced touch 0 Answers