查看: 1886|回复: 7
打印 上一主题 下一主题

[经验分享] 触摸并移动物体对象 iphone开发教程

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-11-23 14:42:00 |只看该作者 |倒序浏览


           非常实用的小教程,基于iphone平台的unity3d基础开发学习教程!
         

           I thought there would be all kinds of examples of how to move an object around in the scene using Touch, but I couldn’t find anything that was simple and straightforward for someone just starting out with Java (like me!).
         

           After digging around in a bunch of different scripts and reading thru a ton a posts on the Unity Forum, I found this post by col000r which had a nice, simple example. I simplified it a little bit so it would work with just 1 attached object.
         

           for(touch in iPhoneInput.touches) {
           

                if(touch.phase == iPhoneTouchPhase.Moved || touch.phase == iPhoneTouchPhase.Began) {
           

                     transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 10));
           

                }
           

           }
         

           That script gets a good bit of the job done, but the object will jump to the position of you finger no matter where you touch the screen.  I wanted the object to only move if you touch it directly.  So I added in the raycast code from my previous Unity iPhone Touch Animation Tutorial (which I found in the iPhone-Match example project).
         

           function Update () {
           

                var hit : RaycastHit;
           

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

                if (Physics.Raycast (ray, hit, 100)) {
           

                     //do something here
           

                }
           

           }
         

           TouchMove Mini-Tutorial:
         

           You can download the  project before you get started to see the completed tutorial or you can follow along with the steps below. This one is pretty simple, so following the steps won’t take long.
         

           1. Create a cube: Go to Game Object –> Create Other –> Cube
         

           2. Add a Box Collider to the Cube: Go to Add Component –> Physics –> Box Collider (if your cube doesn’t already have one)
         

           3. Create a new script: Go to Assets –> Create –> Javascript.  This will create a new script in your Project panel named NewBehaviourScript.  Double-click the script in your Project panel to open it in Unitron.
         

           Copy the following code, paste it into the file, save, and then switch back in Unity.  Drag-and-drop the script from the Project view onto the cube in either the Hierarchy or Scene panel.
         

           function Update () {
           

                var hit : RaycastHit;
           

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

                for(touch in iPhoneInput.touches) {
           

                    if (Physics.Raycast (ray, hit, 100)) {
           

                    if(touch.phase == iPhoneTouchPhase.Moved || touch.phase == iPhoneTouchPhase.Began) {
           

                         transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 10));
           

                    }
           

               }
           

                }
           

           }
         

           4. Be sure to set your iPhone Bundle Identifier: Go to Edit –> Project Settings –> Player and then give it a domain and name.  See Step 16 in the Unity iPhone Touch Animation Tutorail if you need help with this step.
         

           5.  Build and Run: That should do it.
         

           Note that if you have multiple objects with Colliders on them, your cube will move when you click on any of them (bug!).
         

           Feel free to post in the comments improvements to the above script and/or if you have suggestions for how to handle any of the following…
         

           Planned improvements:
           

           – Object should only move if you touch it, not other Mesh Colliders.
           

           – Object should collide with other objects while being moved
           

           – Object should continue to move after you lift your finger, slow over time — like a hockey puck.
           

           – Object should bounce off other objects
           

           – Move multiple objects independently
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-2-7 23:20:38 |只看该作者
长了不少见识
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

板凳
发表于 2012-2-12 23:27:43 |只看该作者
不错不错,收藏了
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-8-6 23:31:45 |只看该作者
顶!学习了!阅!
回复

使用道具 举报

5#
无效楼层,该帖已经被删除
6#
无效楼层,该帖已经被删除
nts    

3

主题

1

听众

743

积分

初级设计师

Rank: 3Rank: 3

纳金币
7
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2013-10-18 18:51:22 |只看该作者
学习了谢谢
回复

使用道具 举报

74

主题

2

听众

3549

积分

中级设计师

Rank: 5Rank: 5

纳金币
-100
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2014-2-10 22:23:24 |只看该作者
相当不错的代理谢谢了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-16 08:23 , Processed in 0.085579 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部