Home Forums Unity SDK 2D homemade GazeAware component Reply To: 2D homemade GazeAware component

#11262
Gregoire
Participant

Ok I finally made it, thanks a lot for your help, you lead me on the way to find the answer.
So I tried your solution which sounded great but actually what unity made with a PollygonCollider2D.bounds.IntersectRay is that they draw a Rect around your sprite which is in the end the exact same thing as putting a BoxCollider on your sprite or use the RectCollider solution above.
So I look online to find a solution which is more a Unity problem than a Tobii, and they propose to use Collider.OverLapPoint(Vector2) // inWorldSpace.
So I needed to transform ScreenPoint given by the GetGazePoint().Screen to WorldPoint.
Camera.main.ScreenToWorldPoint() does that but you need a Vector3 so there is the solution. (I used a tricky opacity animation on gazeFocus to see the different states).

Hope it will help some of you :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Tobii.Gaming;

public class IsColliding : MonoBehaviour {

// Use this for initialization
PolygonCollider2D myCollider;
Camera m_MainCamera;
private Color tmp;
private bool IsCollide;
private bool checkFocus = true;

void Start () {
myCollider = GetComponent<PolygonCollider2D>();
m_MainCamera = Camera.main;
tmp = GetComponent<SpriteRenderer>().color;
tmp.a = 0.49f;
GetComponent<SpriteRenderer>().color = tmp;

}

void Update () {
if(checkFocus){
float zPos = transform.position.z;
Vector3 _gazePoint = new Vector3 (TobiiAPI.GetGazePoint().Screen.x, TobiiAPI.GetGazePoint().Screen.y, zPos);
IsCollide = myCollider.OverlapPoint(m_MainCamera.ScreenToWorldPoint(_gazePoint));
checkFocus = false;
Invoke(“enableCheckFocus”, 0.5f);
}
if(IsCollide){
if(tmp.a <= 1f){
tmp.a += 0.01f;
GetComponent<SpriteRenderer>().color = tmp;
}
} else{
if(tmp.a > 0.5f){
tmp.a -= 0.01f;
GetComponent<SpriteRenderer>().color = tmp;
}
}
}

void enableCheckFocus(){
checkFocus = true;
}
}