- Home /
Help changing mouse input to touch input.
This script works fine but I cannot understand how to make it work with touch input. I have read and watched tutorials, but I cannot seem to get it done. Could someone change this script to touch so I can get forward with learning and with my project. Thank you in advance.
using UnityEngine;
using System.Collections;
public class PauseMenu : MonoBehaviour
{
private bool PopUp;
public bool pause_button = false;
public bool resume_game = false;
public bool quit_to_menu = false;
public bool standard = false;
public bool scroll = false;
public bool time_trial = false;
public bool quitgame = false;
public bool upgrades = false;
void OnMouseDown()
{
if(standard)
{
StartGame.Lvl = 1;
GameObject Sphere = GameObject.Find("Sphere");
Sphere.rigidbody.isKinematic = false;
Sphere.rigidbody.AddForce(0f, 600f, 0f);
}
if(scroll)
{
StartGame.Lvl = 2;
GameObject.Find("Sphere").rigidbody.isKinematic = false;
GameObject.Find("Sphere").rigidbody.AddForce(0f, 600, 0f);
}
if(time_trial)
{
StartGame.Lvl = 3;
GameObject Sphere = GameObject.Find("Sphere");
Sphere.rigidbody.isKinematic = false;
Sphere.rigidbody.AddForce(0f, 600, 0f);
}
if(upgrades)
{
GameObject Cam = GameObject.Find("!Camera");
Cam.animation.Play("CameraLaptop");
GameObject.Find("Laptop/Static").renderer.enabled = false;
StartCoroutine(Up_Delay());
}
if(quitgame)
PopUp = true;
if(pause_button)
{
Time.timeScale = 0.00001f;
GameObject Pausemenu = GameObject.Find("PauseMenu");
foreach(Transform child in Pausemenu.transform)
child.gameObject.SetActive(true);
GameObject PU = GameObject.Find("PowerUps");
foreach(Transform child in PU.transform)
child.gameObject.SetActive(false);
}
if(resume_game)
{
GameObject.Find("Sphere").AddComponent<Resume>();
GameObject Pausemenu = GameObject.Find("PauseMenu");
foreach(Transform child in Pausemenu.transform)
child.gameObject.SetActive(false);
GameObject PU = GameObject.Find("PowerUps");
foreach(Transform child in PU.transform)
child.gameObject.SetActive(true);
}
if(quit_to_menu)
{
Time.timeScale = 1;
Application.LoadLevel(0);
}
}
void DrawInfo()
{
Rect rect = new Rect (300, 225, 350, 150);
Rect Yes = new Rect(350, 325, 80, 40);
Rect No = new Rect(525, 325, 80,40);
if (PopUp)
{
GUI.Box(rect, "Do you really want to exit?");
if(GUI.Button(Yes, "Yes"))
{
PopUp = false;
StartCoroutine(delay(2.0F));
}
if(GUI.Button(No, "No"))
PopUp = false;
}
}
void OnGUI()
{
DrawInfo();
}
IEnumerator delay(float waitTime)
{
yield return new WaitForSeconds(waitTime);
Application.Quit();
}
IEnumerator Up_Delay()
{
yield return new WaitForSeconds(1.11f);
GameObject.Find("Laptop/Upgrades").renderer.enabled = true;
GameObject.Find("Laptop/BorderDown").renderer.enabled = true;
}
}
Comment
Have you tried this script on your touch device? On$$anonymous$$ouseDown() will work with touch, though it can cause a performance hit for handheld devices. Note that if you first converted this code to use Raycasting to detect the mouse hit, the movement to touch would be much smaller.
That or you can explicitly use touch interface code:
void Update$$anonymous$$ouse()
{
if (Input.Get$$anonymous$$ouseDown(0))
{
//todo
}
}
void UpdateTouch()
{
foreach (Touch touch in Input.touches)
{
//todo
}
}
void FixedUpdate()
{
//update whichever based on Application.platform
// or use compile-time vars
}
http://docs.unity3d.com/Documentation/ScriptReference/Input-touches.html