- Home /
2D get touch input
Hello fellow unity developers, I am stuck with a little problem here, the problem is that I don't seem to get any touch input, the purpose of this script is to detect of the position of the player is higher/lower on the x/y axes and then follow an instruction. Determining the player position does work but any touch input doesn't seem to get picked up, Thanks in advance!
This is the script:
function PlayerMove() {
var pos = Camera.main.ScreenToWorldPoint(Vector2(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y));
var playerposx = GameObject.Find("player2").transform.position.x;
var playerposy = GameObject.Find("player2").transform.position.y;
//Debug.Log (playerposx); //display playerpos x in console
//Debug.Log (playerposy); //display playerpos z in console
if (pos.x < playerposx){ // move to the left
rigidbody2D.velocity.x = speed * -1;
}
else {
Debug.Log ("Move to the left doesn't work");
}
}
Are you calling Player$$anonymous$$ove from Update function?
Answer by skylem · Feb 23, 2015 at 10:26 AM
i believe the issue might be that your defining the variable but not actually initiating anything to determine if you have hit the screen i could be wrong but Try this to check
// to set the pos variable when we hit the screen.
if(Input.GetTouch(0)) {
var pos = Camera.main.ScreenToWorldPoint(Vector2(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y));
Debug.Log("pos.ToString()); // this will tell u if the variable
is atleast recieving its correct value
}
hope this helps, if it doesn't let me know and i'll re-assess.
Thanks for the help, unfortunately the code doesn't work, the "Input.GetTouch" can't be called in this script (BCE0026: 'UnityEngine.Touch' cannot be used in a boolean context.)
So I gave it a try it with "if(Input.touchCount > 0)" but this didn't detect any input at all.
ah appologies i assumed that the touch value was a boolean i'll do some playing around and see if i can't deter$$anonymous$$e the type and get back to you.
there appears to be quite a few values regarding Touch i believe this is what your looking for.
public Touch tuch;
if(tuch.phase == TouchPhase.Stationary) {
Debug.Log("is this working?!?!?!");
}
there is also TouchPhase.Canceled, TouchPhase.Began, TouchPhase.Ended TouchPhase.$$anonymous$$oved
if none of this helps let me know
what do you mean with
public Touch tuch;
I assume you meant something like this
var touch:Touch;
both of these are not working.
sorry for the delay, it could be that javascript operates differently sorry if this isn't working for you i don't have any alternative suggestions, as i said before i know very little about touchscreen scripting.
Your answer
Follow this Question
Related Questions
Camera Scripting Help (Sims Style) 0 Answers
How to get an Objects position per axis? 1 Answer
Translate Craig Reynolds's steering behaviors 0 Answers
Moving an object 0 Answers