moving object, using mouse drag

Hi, can someone help me creating a drag scrip to apply to my units? not sure where to get started, since
I can’t find anything that will help me keeping track, if the user hold down left mouse button/drag using finger touch of the object.

thanks alot!
troels

http://unity3d.com/support/documentation/ScriptReference/Input.html

I think you will have to be more clear on what you want. You want to drag a unit? is this unit a person or block or something? Where are you dragging it to?

So if you want a basic click and drag, you would need a GameConroller that handles the drag, and a script that does the OnMouseDown on the actual unit. The Unit talks to teh GameConroller and says… I have been pressed on. Then in the GameConroller’s Update, you keep track of when the mouse is down but only after an event where a unit said that it had been pressed on.

At that point, I would create a plane where the unit was, and do a ScreenPointToRay from the mouse position on it. This will give you a coordinate where the Unit should be. Do a Raycast from far above the map, to the terrain and that will be where the Unit position should be. Thus giving you a drag.

As soon as the mouse is lifted. just simply reset the GameConroller to not capture the move anymore.

Hi, thanks!

well I was planning to move the armies using their own script, not a game controller, unless you guys can tell me it’s a bad plan :slight_smile:

armyscript.js

so basically i need to ask u guys, if its a bad plan to do it this way, and if you can help me with the code itself :slight_smile:

There’s one right in the standard assets that comes Unity(Indie or Pro).

Plus there’s one on unity wiki : unifycommunity.com

If you Google “your question + unity” you’ll usually get an answer.

I honestly think he is trying to make the army move by themselves… after all why would you try to drag each piece one by one. You should be looking more into an RTS game. I think you are trowing us by saying “drag” we are thinking physically drag from one place to another.

And just a tip, you shouldn’t ask other people to right your scripts for.

Its pretty much having someone else make your game for you.

Hi man,

BigMisterB, well it both, im moving some units manually, and with some AI code - but all that is not there, I simply need a
user to CLICK and drag a specific object/block/prefab, using mouse or finger touch, but for some reason i can’t find a solution for this.

I’ll created a large 3d map, using hexagons for a turn by turn strategy game - based on world war 2, this is why i’ll need to find some kind of drag script i can modify to my needs, but I can’t get it to follow the mouse.

To FTheCloud, please read the code above, im not asking other people to write my game, im trying to solve a specific problem, pasting specific code.

I think what people are really asking of you is to try to write it yourself and then come here and ask specific questions with posted code in order to work through whatever problems you’re having.

@ KelsoMRK, yep that’s pretty much what i meant.

@tkjerulf, Plus I already gave you the scripts you wanted dude. Just Use the links I posted.:slight_smile:

Hi gang -

Glad to see this fairly recent thread. I’m fairly new, and am trying to figure a couple of things out about the DragObject script in the Wiki (that FTheCloud referred to). Two things, really:

  1. After dragging my object and letting go, a subsequent click on the object seems to reset it to its original position, rather than letting you drag from the position at which it had been dropped. Would be nice to know where in the script that is controlled.

  2. Which parameter controls the rate and range of motion that the dragged object travels while dragging? The default setting requires a long drag for a really short translation.

Any insight would be GREATLY appreciated.

Thanks!

==rr

Okay, guys, I happen to have this one pretty well figured out, at least for my purposes which involves click drag objects over a flat plane with a snap-to grid. There are several parts.

  1. “Holder.cs” the part that is being moved. this one has the “Holder” script attached to it.
  2. “HolderCollider.cs” sometimes you want the collider to be a different shape than the main parent game object, so I made a separate script for the child object that has the actual collider attached to it. This is optional.
    so we have a game object hierarchy

So now the main GameObject, the parent object, can be an empty game object or character mesh or whatever contains the “Holder.cs” script. If this has a collider then you don’t need HolderCollider.cs.
If you want a separate collider object, then add a child object with the collider, and add the “HolderCollider.cs” script to it.

  1. the “Raycaster.cs” script which pays attention to where the mouse itself is. t/his is separate and can be a component of an empty ggame object.

Note in the background, the ‘floor’ of the world I am not using a terrain, but a plane which is a collider.

Start with the main script, Holder.cs which you put in the parent of the drag drop object:

using UnityEngine;
using System.Collections;

public class Holder : MonoBehaviour 
{
	Raycaster raycaster;

	void Start()
	{
			// Find Raycaster Script:
		GameObject raycasterObject = GameObject.Find("Raycaster");
		raycaster = raycasterObject.GetComponent<Raycaster>();
	}
	public void OnMouseDown()
	{
		Select(true);
		raycaster.Select(this);
	}
	
	public void OnMouseUp()
	{
		raycaster.LetGo();
	}
	
	public void Select(bool isit)
	{ 
                // put special effects here, if you want the object to Glow when it is selected
	}
}

Now add the HolderCollider.cs which you add to the collider game object, which is a child of the main game object.
This is where the collider recognizes the mouse click and passes it on that the object is clicked:

using UnityEngine;

using System.Collections;



public class HolderCollider : MonoBehaviour 

{

	public Holder holder;

	void OnMouseDown()

	{

		holder.OnMouseDown();

	}

	

	void OnMouseUp()

	{

		holder.OnMouseUp();

	}

}

Now we know if we’re clicked or not, and pass the info on to the Raycaster, which moves things:
Raycaster.cs can be attached to an empty game object named “Raycaster”.

using UnityEngine;
using System.Collections;

public class Raycaster : MonoBehaviour 
{	
	
		// Grid data:
	float gridSnap = 2.0f;
	bool dragging;
	RaycastHit hit;
	float adjustedHeight = 0.0f;
	
		// Item data:
	public Holder part;	// the previously selected Part
	Holder partScript;	// the presently selected Part
	
		// Click data:
	float clickStart;
	bool clicked = false;

	void Update () {
		if(clicked)
		{
			if (clickStart < Time.time - 0.2f) 
			{
				clicked = false;
				dragging = true;
			}
		}
		
		if(dragging)
		{
				if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 500.0f, layerMask))
				{
					if(part != null)
					{
						Vector3 worldPosition = hit.point;
						worldPosition.y = adjustedHeight;
						float temp = (hit.point.x - gridSnap/2) % gridSnap + gridSnap/2;
						worldPosition.x = hit.point.x - temp;
						temp = (hit.point.z - gridSnap/2)% gridSnap + gridSnap/2;
						worldPosition.z = hit.point.z - temp;
						part.transform.position = worldPosition;
					}
				}
		}	
	}
	
	
	public void Select(Holder moveablePart)
	{		
		if(moveablePart == null) return;
		
			// first un-select last item:
		if(part != null) 
		if(moveablePart != part)
		{
			part.Select(false);
		}
			// now load the new part:
		this.part = moveablePart;
		
			// and begin the placing procedure:
		//placing = true;
		clicked = true;
		clickStart = Time.time;
	}
		
	public void Deselect()
	{
			// Un-select selected item:
		if(part != null) part.Select(false);
		part = null;
	}
	
	public void LetGo()
	{
		clicked = false;
		dragging = false;
	}
	
}

These are sections from my fully functioning scripts, and I didnt’ test them on their own, but I hope this helps everyone get going in the right direction toward full functioning click and drag action.

The “if (clicked)” part is in the Raycaster script to set the time where it’s not a click and release, but a click and drag. One click won’t work, you have to hold your mouse button down for more than 0.2 seconds for it to work.

This is all me just trying to figure it out. Constructive criticism is welcome. As is feedback on coding / functionality issues. Otherwise enjoy!

Vimalakirti, this looks interesting, thanks for the post . . . I’ll try implementing over the weekend (though I’ll probably need to translate it to JS as well). On first glance, though, it looks like it may be incomplete? I don’t see where translation across the various axes can be controlled, nor the rate . . . but then again, I have yet to dive into Raycasting fully, so maybe the answer lies in there somewhere. Also not sure how practical this approach is if it needs to be applied to potentially hundreds of objects, as may be the case in my game.

Still, the effort and documentation are appreciated, thanks!

Would still appreciate it if someone out there could address my questions re: DragObject as appears on the wiki . . .

Cheers,

==rr