纳金网

标题: 触摸并移动物体对象,iphone开发教程_转载 [打印本页]

作者: 她。    时间: 2012-6-7 16:46
标题: 触摸并移动物体对象,iphone开发教程_转载
非常实用的小教程,基于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 t*** a ton a posts on the Unity F***m, 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



此贴转自:http://www.u3dpro.com/read.php?tid=142
作者: Zack    时间: 2012-11-25 03:40
学习了。谢谢!
作者: may    时间: 2012-12-1 23:35
支持楼主的帖子
作者: 王者再临    时间: 2012-12-2 00:45
中文的解说太少了,,很难理解





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5