加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

基于Unity3d和EasyAR的增强现实系统开发教程(三)

(2019-08-29 11:30:10)
标签:

it

教育

军事

分类: 虚拟及增强现实

基于Unity3d和EasyAR的增强现实系统开发教程实例—AR礼物


EasyAR 开发出一个炫酷的节日礼物效果

预览:

 

开发资源:

链接https://pan.baidu.com/s/1mkWL7Ev7CWOyfp6s4_8JQw 密码:pznt

Step 1:开发环境

关于用EasyAR SDK 搭建AR 开发环境的文章,不懂得朋友可以看下”EasyAR 初学者入门指南(1显示模型。我们直接讲解本次的核心内容。

我们下好资源后,导入到unity,搭建好基本AR环境。如图: 

Step 2:准备模型

我们将准备好的资源礼物与二次元女生导入到unity中,并将三个礼物盒子与女主角拖入到ImageTarget 充当子物体,礼物盒的模型位置在 
 
女主角的模型位置在 
 
拖入之后,根据自己的需求修改其位置,实现其如下效果: 
 

Step 3:编写脚本

首先为礼物盒添加Box Collider,并勾选Trigger 
 
新建脚本,名字随便起,先实现点击礼物盒后,礼物盒消失二次元女生出现,这里用到了一个最巧但最常用方便的方法Void OnMouseDown(),使用这个方法前提是该物体挂了个Collider

void OnMouseDown()

 

{

 

Destroy(this.gameObject);

 

}

 

 

 

Step 4:添加粒子特效

使用粒子特效来使得更令人惊喜的礼物效果,粒子特效的资源位置在 
 
接下来,编写脚本,脚本比较简单,基本思路就是在点击礼物盒子后,盒子销毁,创建粒子特效,代码如下:

using UnityEngine;

 

using System.Collections;

 

public class Explore : MonoBehaviour {

 

public GameObject explore1;

 

public GameObject explore2;

 

public GameObject explore3;

 

public AudioSource sound;

 

// Use this for initialization

 

void Start () {

 

}

 

// Update is called once per frame

 

void Update () {

 

}

 

void OnMouseDown()

 

{

 

Destroy (this.gameObject);

 

Instantiate (explore1,transform.position,transform.rotation);

 

Instantiate (explore2, transform.position, transform.rotation);

 

Instantiate (explore3,transform.position,transform.rotation);

 

}

 

}

 

 

粒子的选择与自己的喜好来选择,不一定和我一样,这样大家可以实现不同的效果。

Step 5:添加音效

音效对一个应用或游戏给人的用户体验影响还是很大的,给礼物盒子添加AudioSource 

using UnityEngine;

 

using System.Collections;

 

public class Explore : MonoBehaviour {

 

public GameObject explore1;

 

public GameObject explore2;

 

public GameObject explore3;

 

public AudioSource sound;

 

// Use this for initialization

 

void Start () {

 

}

 

// Update is called once per frame

 

void Update () {

 

}

 

void OnMouseDown()

 

{

 

Destroy (this.gameObject);

 

sound.Play ();

 

Instantiate (explore1,transform.position,transform.rotation);

 

Instantiate (explore2, transform.position, transform.rotation);

 

Instantiate (explore3,transform.position,transform.rotation);

 

}

 

}

 

 

 

OK,就是这样,用很简单的代码就可以用EasyAR SDK 开发出惊艳的应用。


EasyAR 开发实例—Pokemon Go

EasyAR+Pokemon Go

Pokemon Go 作为去年最火爆的AR游戏除了让用户体验到AR的神奇外,也让开发者兴奋不已。所以了今天给大家分享如何用EasyAR SDK 来构建类似Pokemon Go AR+LBS+IP 的项目。

对于这个较为庞大的项目打算分几期来分享,主要功能或教程目录如下:

1.实现最基础的Pokemon Go 的抛掷效果

2.集成AR录屏功能

3.拍照截屏(录屏)分享朋友圈功能

4.LBS部分,考虑用百度地图/高德地图(或Mapbox)来集成

5.添加语音功能

6.UI部分的设计

7.添加对战功能(精灵PK.AR联机对战)

……..

目前的策划是这样的,当然大家有什么好的想法也可以在下面评论。今天我们来实现第一部分—–抛掷精灵球并捕获皮卡丘。

开发资源:

皮卡丘模型:链接https://pan.baidu.com/s/1mdhfpFnLc6SQFsubz9qn3w 密码:2mnj

游戏音效:链接https://pan.baidu.com/s/1bphIupp 密码: xpbe

Step 1:开发环境

我们将开发资源与EasyAR 2.0 unitypackage 一起导入unity中。框架如图: 
 
在这里为大家准备了很多的PokeBalls 的模型,大家可以自由选择: 
 
 EasyAR_Startup (我们这里没有运用到识别图片之后展现AR模型,所以不需要ImageTarget),皮卡丘模型,pokeballs 都拖入面板中,效果如图: 
 
修改皮卡丘位置与旋转角度(为了获取在移动端的最好体验),大家可以在不断测试中调出合适的数值,例如: 
 
修改Pokeballs 位置(十分重要): 
 
接下来,在皮卡丘上挂上Box Collider,并为其添加Tag(命名为Pika)。 
 

Pokeballs 添加rigidbody  Sphere Collider 

Step 2:
实现抛掷 
我们思路是这样的:点击pokeball ,进行抛物线运动。现在有两种方法来实现,一种是通过Rigidbody来施力实现,另一种是通过transform 来合成加速度实现。

第一种:

Pokeball 上新建一段脚本:

public class ClickSound : MonoBehaviour

{

    bool drawing = false;

    public ParticleSystem par;

    float distance;

    public float ThrowSpeed;

    public float ArcSpeed;

    public float Speed;

 

    void OnMouseDown()

    {

        distance = Vector3.Distance (transform.position,Camera.main.transform.position);

        drawing = true;

    }

    void OnMouseUp()

    {

        this.GetComponent().useGravity = true;

        this.GetComponent().velocity += this.transform.forward * ThrowSpeed;

        this.GetComponent().velocity += this.transform.up * ArcSpeed;

        drawing = false;

    }

    void Update()

    {

        if(drawing)

        {

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            Vector3 rayPoint = ray.GetPoint(distance);

            transform.position = Vector3.Lerp(transform.position,rayPoint,Time.deltaTime * Speed);

        }

    }

}

 

 

 

最主要的是判断点击抬起之后,为其添加向前的推力与向上的动力,来实现其运动。接着,实时判断通过射线来插值的方式实现其运动。

我们设置好向前与向上的速度后,可以实现抛掷效果,但是缺点是不够灵活,很能与皮卡丘进行碰撞检测。

第二种:

借鉴:http://blog.csdn.net/hiramtan/article/details/51753448 
第二种我们通过transform 的方法来实现(直接绑定我们需要与之碰撞的对象即可):

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

public class Throw : MonoBehaviour {

    public const float g = 9.8f;

 

    public GameObject target;

    public float speed = 10;

    private float verticalSpeed;

    private Vector3 moveDirection;

 

    private float angleSpeed;

    private float angle;

 

    bool drawing=false;

    void Start()

    {

        float tmepDistance = Vector3.Distance(transform.position, target.transform.position);

        float tempTime = tmepDistance / speed;

        float riseTime, downTime;

        riseTime = downTime = tempTime / 2;

        verticalSpeed = g * riseTime;

        transform.LookAt(target.transform.position);

 

        float tempTan = verticalSpeed / speed;

        double hu = Mathf.Atan(tempTan);

        angle = (float)(180 / Mathf.PI * hu);

        transform.eulerAngles = new Vector3(-angle, transform.eulerAngles.y, transform.eulerAngles.z);

        angleSpeed = angle / riseTime;

 

        moveDirection = target.transform.position - transform.position;

    }

 

 

    void OnMouseDown()

    {

        drawing = true;

 

    }

 

    private float time;

    void Update()

    {

        if (drawing) {

            time += Time.deltaTime;

            float test = verticalSpeed - g * time;

            transform.Translate (moveDirection.normalized * speed * Time.deltaTime, Space.World);

            transform.Translate (Vector3.up * test * Time.deltaTime, Space.World);

            float testAngle = -angle + angleSpeed * time;

            transform.eulerAngles = new Vector3 (testAngle, transform.eulerAngles.y, transform.eulerAngles.z);

        }

    }

 

}

 

 

 

然后,将对象拖给这段脚本即可: 

Step 3:实现碰撞交互

pokeball 与皮卡丘相碰撞时,我们定义为捕获操作,这是销毁皮卡丘鱼pokeball并播放音效。

首先我们在 pokeball 下面添加“AudioSource”组件 
 
然后在脚本里添加方法:

void OnTriggerEnter(Collider col)

    {

        if (col.tag == "Pika") {

            sound.Play ();

            Destroy (col.gameObject);

            Destroy (this.gameObject,3f);

        }

 

    }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有