- Home /
Question by
lothlorientos · Jul 20, 2014 at 02:21 AM ·
touchperspective camera
Touch isn't working properly.
Hi. I'm having a problem with a game I'm making. It's a 2d game using perspective camera, but for some reason touch doesn't always respond and some time when it does, it doesn't run all the code that it should... The code for touch is:
foreach (var T in Input.touches)
{
var P = T.position;
if (T.phase == TouchPhase.Began && SwipeID == -1)
{
SwipeID = T.fingerId;
StartPos = P;
}
else if (T.fingerId == SwipeID)
{
var delta = P - StartPos;
if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
{
SwipeID = -1;
if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
{
if (delta.x > 0){ //Swipe right
checkTouch (StartPos,1);
}
else if (delta.x < 0){ //Swipe left
checkTouch (StartPos,2);
}
}
else
{
if ( delta.y > 0){//Swipe up
checkTouch (StartPos,3);
if( (grounded == true) && (gamestarted == true))
{
jump = true;
grounded = false;
anim.SetTrigger("Jump");
}
}
else if (delta.y < 0){//Swipe down
checkTouch (StartPos,4);
}
}
}
else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended) //Tap
{
SwipeID = -1;
checkTouch (StartPos,5);
gamestarted=true;
}
}
}
void checkTouch (Vector3 pos,int dir)
{
var ray = Camera.main.ScreenPointToRay(pos);
var hit = Physics2D.GetRayIntersection(ray);
if(hit.collider != null){
if (dir==1) {
hit.transform.gameObject.SendMessage("right",0,SendMessageOptions.DontRequireReceiver);
}
else if (dir==2) {
hit.transform.gameObject.SendMessage("Left",0,SendMessageOptions.DontRequireReceiver);
}
else if (dir==3) {
hit.transform.gameObject.SendMessage("Up",0,SendMessageOptions.DontRequireReceiver);
}
else if (dir==4){
hit.transform.gameObject.SendMessage("Down",0,SendMessageOptions.DontRequireReceiver);
}
else {
if(hit.collider.gameObject.tag == "button")
{
}
}
}
}
And I have code within each of those ifs that do stuff... I'm not sure if this has something to do with framerate an with high detailed textures, so I really lowered the graphics but still not working properly. I really don't know why this is happening.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613154517im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Scripts don't work when i try app 2 Answers
(JS) Touch pause menu issue 1 Answer
Raycast2D with touch in perspective camera. 1 Answer
Unity Touch=2 commends? 1 Answer
Touch returns twice on Android 1 Answer