Hi, Im a newbie, can someone point me in the right direction for: 1. Click and drag an object when finger is pressed on it. 2. Some iPhone scripting resources on the web. Thanks!
If you make your object a rigidbody and apply the DragRigidbody script to the main camera it will do what you want.
ok, but I want it do only be dragable along the xaxis, and that script is large and hard for me to modify..
You say apply the DragRigidbody script… Where do I find this? When I create a new project in Unity iPhone the Project view is empty. Is it supposed to be like this? If I do the same in ‘regular’ Unity, I get two folders ‘Pro Standard Assets’ and Standars Assets’ which is full of stuff. I hope this isn’t a stupid question… I’m completely new to Unity :roll:
ok do this, add the DragRigidbody.js to the maincamera. Your rigidbody that you want to drag make a script (JS) and in Update() add, Code (csharp): transform.position.y = 0; transform.position.z = 0; Attach this script whatever you call it to the object (your rigidbody) you want to move just along the x-axis.
I'm trying to use this script but it's acting "wonky" and i'm not sure it's what I need. First of all, I have multiple rigid bodies in the scene but I only want to drag one of them. How can I do that with this script? Also, it seems like it lags a lot. Instead of dragging, I just really need it to move with the finger on the screen. Any help would be greatly appreciated. Thanks!
There doesn't seem to be much more going on in this subject so I figured I'd post my $0.02. The script below is derived from previous examples, however this will make sure you only drag the object you selected. Also, the object you select will be dragged until yo lift your finger off the screen. If you only want to be able to drag one object, you can put this on an empty game object, camera, etc. and drop the desired object into the "object" slot. For multiple dragable objects, just drop it onto each one (and still drop that object onto the "object" slot). I'm only using single plane dragging (x,y is in the code, just commented out), so I'm sure you will need to tweak the values a bit if you want to enable x,y dragging. Code (csharp): #pragma strict var object : GameObject = null; private var oT : Transform = null; private var selected : boolean = false; function Awake() { if (object) { oT = object.transform; } else { var script : TouchDrag = GetComponent(TouchDrag); script.enabled = false; } } function FixedUpdate () { if (object) { var cam : Camera = Camera.main; for (var touch : iPhoneTouch in iPhoneInput.touches) { var ray = Camera.main.ScreenPointToRay(touch.position); var hit : RaycastHit; if (touch.phase == iPhoneTouchPhase.Began) { if (Physics.Raycast(ray, hit, 100)) { if (hit.collider.gameObject == object) { selected = true; } } } else if (touch.phase == iPhoneTouchPhase.Moved) { if (selected == true) { var cameraTransform = cam.transform.InverseTransformPoint(0, 0, 0); var touchPosition = cam.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cameraTransform.z)); oT.position.x = touchPosition.x; /** * replace the line above with this one to enable full x,y positioning * oT.position = touchPosition; */ } } else if (touch.phase == iPhoneTouchPhase.Ended) { selected = false; } } } } ____________________ -james
motojt, I'm getting the following error on that script in Unity iPhone: Assets/NewBehaviourScript 1.js(13,20): BCE0018: The name 'TouchDrag' does not denote a valid type.
<<Bumping>> Anyone have any updates to this error or suggestions? Would like to see this script in action. I'm not much of a coder, but does the TouchDrag problem a reference to a script or component?
i believe the script itself is named "TouchDrag.js". I think what Awake() is trying to do is find the transform for the object passed in. if it doesn't find it, then disable the script so you don't get any errors or warnings.
Yeah... I've been away for a bit. Vogles is correct in that the name of the script is TouchDrag.js. I actually use this exact code in my game Pinbowl, with exception of one line that is of no consequence. As stated before I just created an empty object and dragged the script onto it. I then dragged the ball (in Pinbowl) into the "object" variable in that empty game object. That object is also home to my static game controller, which is why I decided to do it that way. Also, remember that I am only dragging on the x axis for my game, so to allow x,y you must replace the line as stated in my previous post. Here is the final version as exists in my game: Code (csharp): #pragma strict var object : GameObject = null; private var oT : Transform = null; private var selected : boolean = false; function Awake() { if (object) { oT = object.transform; } else { var script : DragObject = GetComponent(DragObject); script.enabled = false; } } function FixedUpdate () { if (object) { var cam : Camera = Camera.main; for (var touch : iPhoneTouch in iPhoneInput.touches) { var ray = cam.ScreenPointToRay(touch.position); var hit : RaycastHit; if (touch.phase == iPhoneTouchPhase.Began) { if (Physics.Raycast(ray, hit, 100)) { if (hit.collider.gameObject == object) { selected = true; } } } else if (touch.phase == iPhoneTouchPhase.Moved) { if (selected == true) { var cameraTransform = cam.transform.InverseTransformPoint(0, 0, 0); var touchPosition = cam.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cameraTransform.z)); oT.position.x = touchPosition.x; } } else if (touch.phase == iPhoneTouchPhase.Ended) { selected = false; } } } }
I;ve been trying to figure this out for few days and cant get it to work. I have no errors at all.. if I do a trace call inside the fixedupdate() call it keeps calling but not inside if (touch.phase == iPhoneTouchPhase.Began) { or anywhere else. any ideas?? I have a emtpy gameobject with this script on it and dragged in a box with rigidbody physics attached to it
Hi I attached the above script to an empty game object then assigned the main camera to this script. It didn't work as i had hoped..... Any tips? I just want to move the camera along X with finger drag. Cheers
There's an example in this thread showing how to use raycasting to get the world space position of an object under the mouse/finger position onscreen. Having got the world space position, just get its X coordinate and set the draggable object's X coordinate to the same value.
Hi - I too am trying to get a good touch and drag going for a game, but need help with one thing. I've been successful in getting this script to work and added the last 4 lines of code (and two more variables declared at the top): 1. I've found increasing the size of the collider greatly helps in being able to grab an object. In my case, increasing the default size to 1.5 units did the trick. After the touch has released though, and I want to reactivate physics, I reduce the radius back to .5 2. Also, to get accurate touch and drag working your fixed timestep (Edit->Project Settings->Time) needs to be lowered to .01 3. Working on the isKinematic flag right now... I'd like to have isKinematic = true at start so that physics are not controlling the object, and then when the user drops the object for physics to take over... Here's where I'm stuck. It works for one object, but if I have multiple objects on screen that have this script attached (either directly or to an empty GameObject), when I drag and drop one object it turns all objects isKinematic flags to false at once, and not just the GameObject specified in the obj variable?!? Any ideas? Maybe I'm missing something simple? Attached is an example package with touch and drag implemented... [edit] see code below for fix...
I fixed it... Had to do with keeping the object selected... Here's the working script: Code (csharp): #pragma strict var obj : GameObject; private var radius : float; private var oT : Transform = null; private var selected : boolean = false; private var controller : SphereCollider; private var isKinematic : boolean; function Awake() { if (obj) { oT = obj.transform; } else { var script : TouchDrag = GetComponent(TouchDrag); script.enabled = false; } } function FixedUpdate () { if (obj) { var cam : Camera = Camera.main; for (var touch : iPhoneTouch in iPhoneInput.touches) { var ray = Camera.main.ScreenPointToRay(touch.position); var hit : RaycastHit; if (touch.phase == iPhoneTouchPhase.Began) { if (Physics.Raycast(ray, hit, 100)) { if (hit.collider.gameObject == obj) { selected = true; } } } else if (touch.phase == iPhoneTouchPhase.Moved) { if (selected == true) { var cameraTransform = cam.transform.InverseTransformPoint(0, 0, 0); var touchPosition = cam.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, cameraTransform.z)); oT.position.x = touchPosition.x; } } else if (touch.phase == iPhoneTouchPhase.Ended) { if (selected == true) { controller = GetComponent(SphereCollider); controller.radius = 0.5; obj.rigidbody.isKinematic = false; obj.rigidbody.WakeUp(); selected = false; } } } } }
I've made few tutorials on dragging and rotating an object by touch(Unity iOS). These are the Youtube videos: Dragging http://youtu.be/g9p9PvnmRXU Rotating http://youtu.be/oOfPMKdJdKk Get the source code from my website: http://www.revelopment.co.uk Hope it helps.