Click and Drag script

Hi, I have Click and Drag script. However, it currently clicks and drags everything. How can I alter this script to make it only drag a specified GameObject? I’ve tried everything I can think of with my limited knowledge on scripting grammar.

#pragma strict

// Attach this script to an orthographic camera.

private var object : Transform;     // The object we will move.

private var offSet : Vector3;       // The object's position relative to the mouse position.

 

function Update () {

    var ray = camera.ScreenPointToRay(Input.mousePosition);     // Gets the mouse position in the form of a ray.

    if (Input.GetButtonDown("Fire1")) {     // If we click the mouse...

        if (!object) {      // And we are not currently moving an object...

            var hit : RaycastHit;
			
            if (Physics.Raycast(ray, hit, Mathf.Infinity)) {        // Then see if an object is beneath us using raycasting.
	
                object = hit.transform;     // If we hit an object then hold on to the object.

                offSet = object.position-ray.origin;        // This is so when you click on an object its center does not align with mouse position.

            }

        }

    }

    else if (Input.GetButtonUp("Fire1")) {

        object = null;      // Let go of the object.

    }

    if (object) {

        object.position = Vector3(ray.origin.x+offSet.x, object.position.y, ray.origin.z+offSet.z);     // Only move the object on a 2D plane.

    }

}

I’ve continued to research and have found that the following script, that is placed onto the object you want to drag, works best for me:

using UnityEngine;

using System.Collections;

[RequireComponent(typeof(BoxCollider))]

public class Drag : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
private float _lockedYPosition;

void OnMouseDown() {
   //screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position); // I removed this line to prevent centring 
   _lockedYPosition = screenPoint.y;
   offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
   Screen.showCursor = false;
	

	
	      
}

void OnMouseDrag() 
{ 
   Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
   Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
   curPosition.x = _lockedYPosition;
   transform.position = curPosition;
}

void OnMouseUp()
{
   Screen.showCursor = true;
}

}

Here is your script back with a few changes. The code checks for the tag “Draggable”. As mentioned above, you could use the name instead, or even check for a specific component. I reworked your drag code a bit to deal with the offset.

#pragma strict
 
// Attach this script to an orthographic camera.
 
private var object : Transform;     // The object we will move.
private var offSet : Vector3;       // The object's position relative to the mouse position.
private var dist   : float;
 
function Update () {
 
    var ray = camera.ScreenPointToRay(Input.mousePosition);     // Gets the mouse position in the form of a ray.
 
    if (Input.GetButtonDown("Fire1")) {     // If we click the mouse...
 
        if (!object) {      // And we are not currently moving an object...
 
            var hit : RaycastHit;
 
            if (Physics.Raycast(ray, hit, Mathf.Infinity) && (hit.collider.tag == "Draggable")) {        // Then see if an object is beneath us using raycasting.
 
                object = hit.transform;     // If we hit an object then hold on to the object.
 
                offSet = object.position-hit.point;        // This is so when you click on an object its center does not align with mouse position.
                
                dist = (ray.origin - hit.point).magnitude;  // Distance to the object from ray origin.
 
            }
 
        }
 
    }
 
    else if (Input.GetButtonUp("Fire1")) {
 
        object = null;      // Let go of the object.
 
    }
 
    if (object) {
 
        object.position = ray.GetPoint(dist) + offSet;    // Only move the object on a 2D plane.
 
    }
 
}

You can make a check around line 23 for the object’s name, e.g.:

if (Physics.Raycast(ray, hit, Mathf.Infinity)) {        // Then see if an object is beneath us using raycasting.
	
	if (hit.transform.name == "Name of object you want to drag") {
		object = hit.transform;     // If we hit an object then hold on to the object.
		
		offSet = object.position-ray.origin;        // This is so when you click on an object its center does not align with mouse position.
	}
}

This script works for me: Unity Click and Drag script. Add this script and a Collider to an Object. · GitHub

using UnityEngine;

public class Draggable : MonoBehaviour
{
    Vector3 offset;

    void OnMouseDown()
    {
      offset = transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    void OnMouseDrag()
    {
      transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset;
    }
}