read

Let your Unity 3D game scene run in VR head-mounted glasses is easy. First download all related SDK and Unity resource of particular VR glasses, such as Oculus Rift. Then click the Virtual Reality Supported showed in the image below. That’s it.

Leap motion can provide figure gesture control function which is always lack in other VR hand controllers.

Here is an example for how to integrate Leap motion hand gesture interaction method in a tower-defense game. At first, you should download all Unity related developer resources from Leap motion developer website. And then import these assets packages into your Unity game project.

We first create a terrain and beautify it use tools in the terrain inspector window.

You can use brush to paint out different landforms like this.

Paint texture on terrain.

Finally, add grass, tree, or other terrain details.

Add skybox, or other global effects for your game scene are also not so hard. You can do it like the images below.

Unity supports 3D model in a very intuitive way. You can grab 3D models into your game project, and then grab them into your game scene.

Unity uses Mecanim system to control animations in a model. You should always use Animation Controller to control the animation clips in a model.

It’s time to add AI function to an enemy model. Here the AI is mainly path finding.

In order to make AI effective, you should add NavMeshAgent to AI models like this.

It’s time to hit the script, use a script to give them destination. Create an empty game object as the destination.

The script can be written like this. Grab this script into the enemies.

    public Transform destPoint;                             
    private NavMeshAgent nav;
    void Awake()
    {
        nav = GetComponent<UnityEngine.AI.NavMeshAgent>();  
        if (!nav)
            return;
        Init();
    }
    public void Init()
    {
        nav.SetDestination(destPoint.position);     // set nav destination
    }

Now, we are moving to Leap motion stuff. Leap motion supports two kinds of methods for hand gesture interaction. The fist is traditional one, the other is VR one. The latter one requires Leap motion sensor stick in the front of the VR glasses.

In software end, after downloading and importing the Leap motion Unity assets, you can grab the LMHeadMountedRig into the game scene as the VR camera and Leap motion hand controller.

Configurator hand pool under the LMHeadMountedRig game object just grabbed.

Add graphic and physical hand models to it, and set model pool to 2 like this.

Now, it’s time to do real hand control things. We should add hand gesture event system into LMHeadMountedRig.

We should follow these steps to make it works.

  1. Create an empty game object
  2. Grab it to graphic right hand as its child
  3. Add ExtendedFingerDetector to it
  4. Configure this script component

We then add a player game object to do interaction. The script hanged in player game object written like this.

   public GameObject bullet;
    [SerializeField]
    public Leap.Unity.ExtendedFingerDetector handFinger;
    public void Fire()
    {
        Hand hand;
        hand = handFinger.HandModel.GetLeapHand();
        float[] f = hand.PalmNormal.ToFloatArray();
        Vector3 handNormal = new Vector3(f[0], f[1], f[2]);
        float[] p = hand.PalmPosition.ToFloatArray();
        Vector3 handPos = new Vector3(p[0], p[1], p[2]);
        GameObject.Instantiate(bullet, handPos, Quaternion.FromToRotation(Vector3.forward, handNormal.normalized));
    }

We should also do some coding to control the bullet.

public float speed = 10.0f;
    private Rigidbody rb; // rigid body component of this bullet
    void FixedUpdate()
    {
        rb.MovePosition(transform.position + transform.forward * speed * Time.deltaTime);
    }
    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

We can just create a simple bullet prefab.

Then we grab the bullet prefab into the player parameter.

Finally, the magic thing comes. We use a hand gesture, palm means all fingers extended, to start fire. And use palm direction as fire direction. We can do it even without coding, just use hand event system provided by Leap motion.

We can add some UI staff as well.

Use script to control UI elements.

    void Start()
    {
        gameInfo = GameObject.Find("Text").GetComponent<Text>();
    }
    // Update is called once per frame
    void Update()
    {
        gameInfo.text = "Kill enemies:" + "\n" + "Lost enemies:" + "\n"
    + "Weapon:" + "\n" + "Ammo:" ;
    }

We should also implement some game logics. Such as Bullet collider with enemies, and enemy behaviors.

public PlayerFire p;
    public void ShootMe()
    {
        p.killed++;
        Destroy(gameObject);
    }

Bullet collider behavior can like this.

public PlayerFire p;
void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            EnemyAI eai = collision.gameObject.GetComponent<EnemyAI>();
            eai.ShootMe();
        }        
    }

More details, please visit the demo project here.

Blog Logo

Honglei Han 韩红雷


Published

Image

Honglei Han 韩红雷的主页

Honglei Han from CUC 中国传媒大学 游戏设计 韩红雷 科研信息 所带课程 个人介绍 开源项目 创作的部分古体诗 Unity 3D 游戏引擎 Game Engine 游戏开发 计算机图形学 虚拟现实 Virtual Reality 程序设计 Programming

Back to Overview