Any scripts for: Touch to select object, drag to move object

Hi Community,

I’m working on an iPhone game that requires a “touch” to select and a “drag” to translate the object in the direction you drag on the screen. So far it’s only 2D but the plan is to eventually let you translate in 3D space relative to the camera position.

Anyone know where I can find a script to do that or use as a starting point?

Thanks in advance for your help.

Cheers,

Josh

Hey Josh,

I just had to figure out something similar, and actually just posted to the forum making sure what I’m doing is proper and all that. It works for me, however, so maybe you can use it as a starting point unless others post something better.

var hit : RaycastHit;
var object : GameObject;
var buttonEmitter : ParticleEmitter;

function Update () {
	
	// Use Raycast to pick objects that have mesh colliders attached.
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	
	//if the user touches the button
	if (Input.GetMouseButtonUp (0))  //Returns true during the frame the user deselects the object
	{
		if (Physics.Raycast (ray, hit, 100)  hit.transform.name == object.name) 
		{
			ButtonTouchUp(object);
		}
	}

}

I just attach this script to a GameObject, and I also assign the “object” variable to the game object I’m attaching the script to. Then I compare the hit.transform.name to the object I touched to make sure they match. I had to do this because, from what I can tell from the script reference, raycasting will hit every collider on the screen. So I compare the one that was hit with my GameObject’s name, then if that’s true I call a custom method to do some stuff, like change the transform, etc.

Also, if you lookup iPhoneInput.GetTouch in the script reference there’s an example in there that moves a game object via the player’s touch.

Hope that helps!

I’ll give the script a try. Thanks for your help.

Cheers,

Josh

@joshmorris3d how does it work :wink:

I’ve made a tutorial for dragging an object by touch(Unity iOS), I think it is what you’re looking for.

Here’s the Youtube video:

Get the source code from my webiste:

http://www.revelopment.co.uk/tutorials/unitytutorials/77-unitydragtutorial

Hope it helps.