查看: 1000|回复: 2

[脚本] IOS触摸并移动物体对象

[复制链接]

36

主题

12

听众

200

积分

小学生

Rank: 2

升级   0%

发表于 2012-12-16 16:02 |显示全部楼层
共享资源获取更多积分!share your resources get more credits!
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).
  1. function Update () {
  2.      var hit : RaycastHit;
  3.      var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  4.      if (Physics.Raycast (ray, hit, 100)) {
  5.           //do something here
  6.      }
  7. }


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.

  1. function Update () {
  2.      var hit : RaycastHit;
  3.      var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  4.      for(touch in iPhoneInput.touches) {
  5.          if (Physics.Raycast (ray, hit, 100)) {
  6.          if(touch.phase == iPhoneTouchPhase.Moved || touch.phase == iPhoneTouchPhase.Began) {
  7.               transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, 10));
  8.          }
  9.     }
  10.      }
  11. }


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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

星期天 星期一 过度很痛苦

0

主题

8

听众

201

积分

小学生

Rank: 2

升级   0.33%

发表于 2013-2-24 11:32 |显示全部楼层
楼主太有才了,膜拜中……
回复

使用道具 举报

0

主题

8

听众

201

积分

小学生

Rank: 2

升级   0.33%

发表于 2014-7-11 22:02 |显示全部楼层
膜拜中……膜拜中……
回复

使用道具 举报

0

主题

8

听众

202

积分

小学生

Rank: 2

升级   0.67%

发表于 2017-3-12 12:59 |显示全部楼层
顶顶多好
回复

使用道具 举报

0

主题

8

听众

201

积分

小学生

Rank: 2

升级   0.33%

发表于 2017-3-12 13:39 |显示全部楼层
难得一见的好帖
回复

使用道具 举报

0

主题

8

听众

202

积分

小学生

Rank: 2

升级   0.67%

发表于 2017-3-12 13:15 |显示全部楼层
说的非常好
回复

使用道具 举报

1

主题

9

听众

202

积分

小学生

Rank: 2

升级   0.67%

发表于 2017-3-12 12:52 |显示全部楼层
很好哦
回复

使用道具 举报

1

主题

9

听众

202

积分

小学生

Rank: 2

升级   0.67%

发表于 2017-3-12 13:23 |显示全部楼层
不错不错
回复

使用道具 举报

0

主题

8

听众

200

积分

小学生

Rank: 2

升级   0%

发表于 2017-3-24 20:24 |显示全部楼层
很不错
回复

使用道具 举报

0

主题

8

听众

200

积分

小学生

Rank: 2

升级   0%

发表于 2017-3-24 20:16 |显示全部楼层
楼主是超人
回复

使用道具 举报

温馨提示:求助请到“Unity技术讨论”版块中发帖,便于集中解决!
您需要登录后才可以回帖 登录 | 立即注册

懒人模式:

关闭

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

Unity3D游戏开发联盟(资源共享平台) -Unity3D,Unreal ( 苏ICP备12051100号 )   

GMT+8, 2017-11-25 11:34 , Processed in 0.099550 second(s), 42 queries .