How to drag an object

Hey guys,

I’m using this code to allow me to drag my objects in my game scene:

// Moves object according to finger movement on the screen
var speed:float = 0.1;
function Update () {
    if (iPhoneInput.touchCount > 0  iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Moved) {
    
        // Get movement of the finger since last frame
        var touchDeltaPosition:Vector2 = iPhoneInput.GetTouch(0).deltaPosition;
        
        // Move object across XY plane
        transform.Translate (touchDeltaPosition.x * speed, touchDeltaPosition.y * speed, 0);
    }
}

The problem is, that it does not matter where I put my finger on the screen the object is still selected, as you can imagine this will be a problem when the scene has multiple objects.

Is there a simple way to edit the code to make sure the users finger is actually on the object before allowing them to drag it?

Thanks

I’m stuggling to get past this, people have mentioned raycasting before on the forums, could this be appropriate?

There is a simple raycast example in this thread. Also, you might want to look at adding an OnMouseDown function to the draggable object’s script.

Thanks andeeee that worked nicely.

I’m having a problem implementing the draging part of the script, heres what I mangaged to do:

http://pastie.org/1014997

Thanks

I still cant seem to implement the dragging action, any thoughts on how to tackle this?

Thanks

I’ve attached a script that shows a way to drag an object in the plane of the camera. You can add it to any convenient object in the scene.

332885–11714–$dragobj_313.js (818 Bytes)

Thanks that works great :slight_smile:

The only problem is that all objects become draggable, not just the one it is attached to?

Any ideas on this ?

Thanks :slight_smile:

Heres a video of the problem : http://tiny.cc/2v6ux

As you can see , as I move my mouse across the wall I seem to somehow allow cubes to pass through them -as if I’m moving the collider?

Although I can also move cubes ( which is the part I want)

I really dont know whats wrong with the code. I have added in 2 extra checks and that did not make a difference.

private var currTarget: Transform;
private var dragging: boolean;
private var clickOffset: Vector3;


function Update () {
	var objPlane: Plane;
	var hitDist: float;
	var hit: RaycastHit;


			if (Input.GetMouseButton(0)  gameObject.name =="enemy")  {
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);

		if (dragging) {	
			objPlane = new Plane(Camera.main.transform.forward, currTarget.position);
			objPlane.Raycast(ray, hitDist);
			currTarget.position = ray.GetPoint(hitDist) + clickOffset;
		} else if (Physics.Raycast(ray, hit)  Input.GetMouseButtonDown(0) gameObject.name =="enemy") {
			dragging = true;
			currTarget = hit.transform;
			
			objPlane = new Plane(Camera.main.transform.forward, currTarget.position);
			objPlane.Raycast(ray, hitDist);
			clickOffset = currTarget.position - ray.GetPoint(hitDist);
		}
	} else {
		dragging = false;
	}
}

Sorry, I should have thought of that…

Anyway, the best place to check is on the “else if” line:-

else if (Physics.Raycast(ray, hit)  Input.GetMouseButtonDown(0)  hit.gameObject.name == "enemy") { ...

This is how you would identify the target by name, but you could just as easily do it by the tag, etc.

Thanks andeee, I seem to be getting this error when I make the change:

‘gameObject’ is not a member of ‘UnityEngine.RaycastHit’

D’Oh! Sorry, it should be “hit.transform.name”, not “hit.gameObject.name”.

Andeeee: Im struggling a bit to convert your nice dragObject script into something I can use for my Xoom.

I changed something in your script here:

function OnMouseDown ()
{
	[B]if(Input.touchCount > 0)[/B]
	{
	canMove = true;
	myTransform.Translate(Vector3.up*addHeightWhenClicked);
	gravitySetting = myRigidbody.useGravity;
	freezeRotationSetting = myRigidbody.freezeRotation;
	myRigidbody.useGravity = false;
	myRigidbody.freezeRotation = freezeRotationOnDrag;
	yPos = myTransform.position.y;
	}
}

I have a vague hunch about leaving out the entire OnMouse… functions, but struggling with understanding the Touch part of UNity.
Any pointers are really welcomed!

Edit: I tried throwing in this code I found in the reference:

// Moves object according to finger movement on the screen

var speed : float = 0.1;
function Update () {
if (Input.touchCount > 0 
Input.GetTouch(0).phase == TouchPhase.Moved) {

// Get movement of the finger since last frame
var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;

// Move object across XY plane
transform.Translate (-touchDeltaPosition.x * speed,
-touchDeltaPosition.y * speed, 0);
}
}

My objects are touchable, but they just fly like little mentals :slight_smile: