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;
}
}
}
}
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().
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.
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;
}
}
}
}