Forgot password?
 立即注册
EasyAR Portal Tutorial View Content

MultiTarget recognition- dual target battle

AlbertLee 2017-8-3 09:38

EasyAR Basic Tutorial--MultiTarget

This case is battle1.0 with dual target which is suitable for beginners in AR developing. We will present a complete AR battle project later.

Preview:


Developing description:

When two target images touch with each other, two monsters will play animation respectively 

code: https://drive.google.com/open?id=0B6vjKDkZUGVsZ1UzcFR4Q1FWM0k .

Resources: https://drive.google.com/open?id=0B6vjKDkZUGVsczJEa1MtdlJuN0k


Step 1: Set up developing environment 

Previously, we had learned how to develop multi-image recognition with EasyAR SDK. And this case is based on the last one. There is no doubt that we can develop it based on EasyAR official demo (These two methods are much the same). Our previous project structure is like this:


Import necessary model resources into our project. The directory structure is like below:



Step 2:Set up the model

Replace Cube with our prepared model and modify its Scale and rotation in a certain degree. The picture is presented below: 



Set the parameters for these two models respectively. Firstly set the Scale and Rotation for the model of Image Target.


We need to modify its Animation. In this case, we only need "n2017_idle"  and “n2017_skill_2". The picture is presented below:


Set Wrap Mode as "Loop"

Another model’s set is the same, so you can set up yourself. The necessary Animations are “Standby” and “Attack3”.



Then, add Box Collider to both models, tick “Is Trigger”. We need to set up the size of Box Collider now, makes the length slightly bigger than the width. For a more convenient collision test, I can give you a reference parameter.



For using OnTriggerEnter, we need to add a “RigidBody” to the body.




Step 3:Write Script

  • http://www.taikr.com/course/445
  • http://www.taikr.com/course/34

Create a Label:


change the label content:


the following:



add Tween-Alpha


Alpha from 0 to 1,Duration:2s。

set the label's active false


the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayAnim : MonoBehaviour {

    public TweenAlpha label;
    // Use this for initialization
    void Start () {
        
    }  
    // Update is called once per frame
    void Update () {    
    }
    void OnTriggerEnter(Collider col) 
    {    
        label.gameObject.SetActive (true);
        label.PlayForward ();
    }

}
  

Hide();


public void Hide()
    {
        label.gameObject.SetActive (false);
    }



We can create a script based on model to achieve video interaction. The codes are relatively simple, as the basis in Unity.

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

public class PlayAnim : MonoBehaviour {
    private Animation anim;
    // Use this for initialization
    void Start () {
        anim = GetComponent<Animation> ();
    }  
    // Update is called once per frame
    void Update () {    
    }
    void OnTriggerEnter(Collider col) 
    {
        anim.Play ("n2017_skill_2");
        col.gameObject.GetComponent<Animation> ().Play ("Attack3");
    }
}

Then, our testing is done. We will keep optimizing AR Battle’s developing in the future tutorial.