- Home /
how i can convert thjs code to touch (Input.anyKeyDown)
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
// Use this for initialization
void Start () {
isStartButtonPressed = false;
Time.timeScale = 0.0f;
}
// Update is called once per frame
void Update () {
updateScore();
if(!isInView())
{
restartGame();
}
if(Input.anyKeyDown)
{
move();
}
}
private bool isInView()
{
Vector3 port = Camera.main.WorldToViewportPoint(transform.position);
if((port.x < 1) && (port.x > 0) && (port.y < 1) && (port.y > 0) && port.z > 0)
{
return true;
}
else
{
return false;
}
}
private bool isStartButtonPressed;
public GUIText scoreLabel;
void OnGUI()
{
if (!isStartButtonPressed)
{
GUI.TextField(new Rect(Screen.width/2-65, Screen.height/2-11 ,130,22), "Do something to start");
if(Input.anyKeyDown)
{
Time.timeScale = 1.0f;
isStartButtonPressed = true;
}
}
}
private void move()
{
rigidbody.velocity = new Vector3(0,0,0);
rigidbody.AddForce (new Vector3(275,200,0), ForceMode.Force);
}
void OnTriggerEnter(Collider other)
{
restartGame();
}
private void restartGame()
{
Time.timeScale = 0.0f;
isStartButtonPressed = false;
Application.LoadLevel (Application.loadedLevelName);
}
private void updateScore()
{
int score = (int) (transform.position.x / GenerateWorld.distanceBetweenObjects);
if(score != (int.Parse(scoreLabel.text)) && score > 0)
{
scoreLabel.text = score.ToString();
}
}
}
That is a lot of code to go through. Is there an issue with all of this code? Could you highlight where in the code the issue is? What is the precise issue you are seeking help with?
Answer by robertbu · May 15, 2014 at 01:31 PM
Your input is very simple here. You need to make the "Do something to start" go away, and you need to call move() when a single event occurs. You can do the first by checking Input.toucCount. If it is greater than 0, then make the the text go away. Change line 41 from:
if(Input.anyKeyDown)
to:
if(Input.touchCount > 0)
For the move(), you likely want to detect a finger down event. You can do that by checking the touch inputs and look for the begin phase. Take a look at the second example script on this page here:
https://docs.unity3d.com/Documentation/ScriptReference/Input.GetTouch.html
Your code, instead of calling Instantiate() would call move().
Answer by hari ram · May 15, 2014 at 02:04 PM
Your almost there & need to change just a few things.
Check-out a similar answer posted by me in the following page....
Your answer
Follow this Question
Related Questions
I need touch input example 1 Answer
[HELP] Joystick Movement/Button Movement 0 Answers
on touch play animation in android devices. 1 Answer
iOs/Android Development 2 Answers