x


iPhone Droid Input.GetTouch(i).position

Now either Unity3D is full of Bugs or I am missing something simple. The Texture versus the Touch event do not even remotely line up to the same position to fire off an event. Using a simple pair of 32x32 images to invoke seperate events but the input position seems to be miles away even when I do a contain.

NOTE: Not using Event.current.mousePosition as it is for a single input event and using two fingers on a mobile and not one. Also, Guitexture is not an option for this since i'm using the ultimate FPS camera and it hates guitextures.

    using UnityEngine;
using System.Collections;

public class touch : MonoBehaviour
    {
    public Texture2D jumpicon;
    public Texture2D forwardicon;
    public GUISkin White;
    public int jumpxpos;
    public int jumpypos;
    public int forwardxpos;
    public int forwardypos;

    void OnGUI()
       {
       Rect r = new Rect(Screen.width-jumpxpos,Screen.height-jumpypos,32,32);
       GUI.DrawTexture(r, jumpicon);

       Rect rforward = new Rect(Screen.width-forwardxpos,Screen.height-forwardypos,32,32);
       GUI.DrawTexture(rforward, forwardicon);

       if(Input.touches.Length > 0)
         {
         int i = 0;
         while (i < Input.touchCount) 
          {  
          Vector2 fingerPos = Input.GetTouch(i).position;
          if(r.Contains(fingerPos))
              {
              Debug.Log("jump");
              }
          if(rforward.Contains(fingerPos))
              {
              Debug.Log("forward");
              }       
          ++i;
          }
         }
       }
    }
more ▼

asked Jun 01 '12 at 09:21 PM

urawhat gravatar image

urawhat
11 1 1 2

It's probably not a good idea to be doing touch events in OnGUI - especially as that code is executed multiple times for different events. Not sure that's causing your problem as you are explicitly setting the rectangles, but given that Input.mousePosition doesn't work in OnGUI, it's probably reasonable to believe that the touch code is unreliable too.

Jun 01 '12 at 11:58 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Ouch. Been searching the web and just not finding the correct way. Any links that might point me in the correct path?

more ▼

answered Jun 02 '12 at 12:36 AM

urawhat gravatar image

urawhat
11 1 1 2

Hang on let me dig something out - I was searching when you posted this.

Just one thing - and dont worry everyone does it first time - but don't post comments as answers :) In Unity Answers "Answer" means "Solution" and not "Reply". There's a add new comment link hidden on the right of the screen.

Answers like this get deleted by someone - or possibly turned into a comment depending on how busy the person doing it is :)

Jun 02 '12 at 12:38 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

So actually I think your code for the detection would be fine if it wasn't in OnGUI. Given that you could store the rectangles as private vars rather than local variables you could draw your textures in OnGUI and actually detect your collisions in Update().

more ▼

answered Jun 02 '12 at 12:42 AM

whydoidoit gravatar image

whydoidoit
33k 11 23 100

In addition you could use GUITextures and real objects with colliders parented to the main camera or a secondary active camera then use a ray cast and colliders.

There is this code that makes OnMouseDown work for multiple fingers on real objects with colliders:

//  OnTouchDown.cs
//  Allows "OnMouseDown()" events to work on the iPhone.
//  Attach to the main camera.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class OnTouchDown : MonoBehaviour
{
    void Update () {
        // Code for OnMouseDown in the iPhone. Unquote to test.
        RaycastHit hit = new RaycastHit();
        for (int i = 0; i < Input.touchCount; ++i) {
            if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) {
            // Construct a ray from the current touch coordinates
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
            if (Physics.Raycast(ray, out hit)) {
                hit.transform.gameObject.SendMessage("OnMouseDown");
              }
           }
       }
    }
}
Jun 02 '12 at 12:47 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

I will have to mess around with this. GUITexture not an option, but if u did a drawtexture do you think it would work? Hardest part is insuring it only does the event when the finger is only on the texture.

more ▼

answered Jun 02 '12 at 10:12 AM

urawhat gravatar image

urawhat
11 1 1 2

Just keep you Rects public and it should be fine

Jun 02 '12 at 10:13 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

Well, i know this is a little old post, but i think it worth to answer this, since I have been searching a lot to find this answer.

Your first code works fine, you need to take out your code for detection as whydoidoit suggested, but there is one little thing that is missed there, you need to substract the fingerPos.y form Screen.height (after searching, the touch position in the y axis is flipped or backwards in respect to screen.height), the code would be something like this:

  using UnityEngine;
using System.Collections;

public class touch : MonoBehaviour
    {
    public Texture2D jumpicon;
    public Texture2D forwardicon;
    public GUISkin White;
    public int jumpxpos;
    public int jumpypos;
    public int forwardxpos;
    public int forwardypos;
 Rect r;
Rect rforward;
    void OnGUI()
       {
       r = new Rect(Screen.width-jumpxpos,Screen.height-jumpypos,32,32);
       GUI.DrawTexture(r, jumpicon);

       rforward = new Rect(Screen.width-forwardxpos,Screen.height-forwardypos,32,32);
       GUI.DrawTexture(rforward, forwardicon);


    }

void Update(){
if(Input.touches.Length > 0)
         {
         int i = 0;
         while (i < Input.touchCount) 
          {  
          Vector2 fingerPos = Input.GetTouch(i).position;
          fingerPos.y = Screen.height - fingerPos.y;
          if(r.Contains(fingerPos))
              {
              Debug.Log("jump");
              }
          if(rforward.Contains(fingerPos))
              {
              Debug.Log("forward");
              }       
          ++i;
          }
         }
       }
}

Hope this would help someone.

Rhod,

more ▼

answered Mar 02 at 04:50 AM

rhodnius gravatar image

rhodnius
101 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2464
x953
x885
x71
x6

asked: Jun 01 '12 at 09:21 PM

Seen: 1153 times

Last Updated: Mar 02 at 04:50 AM