Drag'n Drop System

Hi there! I am building a 3D drag’n drop game. I have a computer and components on the shelf to drag’n drop into the case. I’ve implemented this script:

using UnityEngine;
using System.Collections;

public class drag2 : MonoBehaviour
{  
	private bool _mouseState;
	public GameObject Target;
	public Vector3 screenSpace;
	public Vector3 offset;

	// Use this for initialization
	void Start ()
	{

	}

	// Update is called once per frame
	void Update ()
	{
		// Debug.Log(_mouseState);
		if (Input.GetMouseButtonDown (0)) {
			RaycastHit hitInfo;
			if (Target == GetClickedObject (out hitInfo)) {
				_mouseState = true;
				screenSpace = Camera.main.WorldToScreenPoint (Target.transform.position);
				offset = Target.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
			}
		}
		if (Input.GetMouseButtonUp (0)) {
			_mouseState = false;
		}
		if (_mouseState) {
			//keep track of the mouse position
			var curScreenSpace = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);

			//convert the screen mouse position to world point and adjust with offset
			var curPosition = Camera.main.ScreenToWorldPoint (curScreenSpace) + offset;

			//update the position of the object in the world
			Target.transform.position = curPosition;
		}
	}


	GameObject GetClickedObject (out RaycastHit hit)
	{
		GameObject target = null;
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		if (Physics.Raycast (ray.origin, ray.direction * 10, out hit)) {
			target = hit.collider.gameObject;
		}

		return target;
	}
}`

It works fine but I want to be possible to move objects around the z axis too. Do anyone know how to achieve that? Could be by changing the z-axis by mousewheel or something like that.

Then I’ve implemented a drop areas (there are hidden objects and Box collider triggers on them) which are being active when the user drag the right object there. This is my script:

using System.Collections;
using UnityEngine;

public class Enter : MonoBehaviour {

	public GameObject placingObject;
	public GameObject placedArea;

	private void OnTriggerEnter(Collider col) 
	{
		string name1 = placingObject.name+"_placed";
		Debug.Log (name1);
		string name2 = placedArea.name;
		Debug.Log (name2);
		bool control = false;
		if (name2 == name1 && !control) {
			control = false;
			placedArea.SetActive (true);
			placingObject.SetActive (false);
		} else {


		}
	}
}

There is one big mistake. When I drag object and it hits the trigger of another area than I want, the object of this area is being active and my dragged object is still “in my hand”. There shloud be some restriction to drop only objects which belongs to exact place. How to do that?

Next, I want to move objects back to start position when the user don’t place the objects in the exact place. Again, I don’t know how to achieve that.

Next, I want to change camera on click to button. Should I do it by only setting active camera (it will be not okay with my drag’n.drop script because of main.camera setting) or by changing the animation and position of main camera only?

The last thing is: I want to create some control system. It will add right placed objects to the array and the game will react (with some messages to user) if he placed all components or something like that. However, I don’t know where to put this code (to drag’n drop or to Trigger script?

Thank you very much for your advices :slight_smile:
Have a great day!

Jakub

I think is something like on RTS games that u r seeking look here (Mini Unity Tutorial - Pick Up & Place Objects - Beginners - YouTube)