Maxime FRAPPAT

Hum …no thanks ! – Lordinaire

Tag: unity (Page 2 of 6)

[Unity] Using Tobii EyeX Controller to interact with your game

Tobii EyeX Controller is an awesome product who give you the power with… your eyes ! I’m not joking, you can control things with your eyes and it works pretty well :) The firm who  product this controller also made a SDK for Unity so let’s play with him !Tobii_Device

How it works ?

With several sensors, the controller is able to track your eyes position to know where you are looking at. You just need to put the device at the bottom of your screen and to calibrate it! Ready to rock ?

Example

Create a new project in Unity  (any recent version will work).

Download the SDK

You can find the SDK here : http://developer.tobii.com/downloads/

unity_download_sdk

 Install the package

Unzip the downloaded file and double click on EyeXFramework.unityPackage. This will open a popup to import the package in your Unity project.

Interactive sphere

Select GameObject > Create Other > Sphere.

On that sphere, we will add 3 components :

  • Gaze Aware (from Tobii SDK)
  • Particle system
  • Custom script to show particles only when you look at the sphere

Gaze Aware

Select the sphere and in the Inspector panel, select Add component > Tobii EyeX > Gaze Aware. This script sets the sphere as reactive to the sensor, we can now know when the player focus the sphere.

Particle system

Select the sphere and in the Inspector panel, select Add component > Effects >Particle System. Set the property Start Lifetime to 1 and disable the property Emission.

Custom script

Once again, select the sphere and in the Inspector panel, select Add component > New script > Csharp > Create and Add. Open the script and copy this code snippet inside :

using UnityEngine;
using System.Collections;

public class ParticleBehavior : MonoBehaviour
{
	private ParticleSystem _particleComponent;
	private GazeAwareComponent _gazeAwareComponent;

	// Use this for initialization
	void Start ()
	{
		_particleComponent = GetComponent<ParticleSystem>();
		_gazeAwareComponent = GetComponent<GazeAwareComponent>();
	}

	// Update is called once per frame
	void Update ()
	{
		if (_gazeAwareComponent.HasGaze)
		{
			_particleComponent.enableEmission = true;
		}
		else
		{
			_particleComponent.enableEmission = false;
		}
	}
}

We use the property HasGaze() of GazeAwareComponent to determinate whether the sphere is focus or not. The property enableEmission of the class ParticleSystem is use to active the particle system.

unity_particles_playing

 

Have fun !

[Unity] Replay the same animation during its execution

When you use animators and animations in your game, you may want to be able to play an animation even if it’s already running.

To explain how to do this, we will create a scene with a simple square object. This square will have an animator component attached to it.

With transition

You can achieve an animation with transition by playing it with the CrossFade method.

Let’s create a new animation like this one :

animation_with_transition

Add a new C# script component attached to the cube object :

public class MoveBehavior : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 70, 50, 30), "Replay"))
        {
            var animator = GetComponent<Animator>();
            animator.CrossFade("Move", 1f);
        }
    }
}

If you play the scene and click on the “Replay” button, the animation will start.

Notice that if you click on the button again between 0 and 1 second, the animation will start transition, but if you click between 1 and 2 seconds nothing will happen. It’s because our animation during 2 seconds and we set the fading length to 1 second. To be able to play a transition all along the animation, set the fading length to 2 seconds.

Without transition

Unlike the CrossFade method, the Play method will not rewind to the beginning. So, if you want to replay an animation you can use a little trick.

Create your animation in two parts. The first part need to be a one-frame animation :

first_animation_without_transition

The second part is the “real” animation :

second_animation_without_transition

Edit the script with those snippet :

public class MoveBehavior : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 70, 50, 30), "Replay"))
        {
            var animator = GetComponent<Animator>();
            animator.Play("BeginMove");
        }
    }
}

By playing the BeginMove animation, it will transition to the EndMove after only one frame. So, if you play again BeginMove after 1 second, the animator current animation will be EndMove so you will be able to replay the animation to the beginning :)

Have fun !

Page 2 of 6

Powered by WordPress & Theme by Anders Norén

%d bloggers like this: