Object grabbing system doesn't always work on first click.

So I’ve been using this script for a little while and it works for the most part. It’s a bit finicky, as it sometimes works with a single click and other times works after a couple of clicks on the object I’m trying to “grab” with the mouse.

Here’s the code :

#pragma strict

//==========================================================================================================================//
var mstrCtrl : GameObject;
var selected : GameObject;  //should set to whatever game object gets clicked on and move it
var selectedOn : boolean;
var nextUse : float;
var delay = 0.5; //seconds delay
var distance = Vector3;
//=========================================================================================================================//

function Start () {

nextUse = 1;
mstrCtrl = this.gameObject;
DontDestroyOnLoad (mstrCtrl);
selectedOn = false;
selected = null;
}

function Update () { 

var V3T = Vector3(Input.mousePosition.x, Input.mousePosition.y, 10); 
V3T = Camera.main.ScreenToWorldPoint(V3T);
	
	if(selectedOn == false){
			if(Input.GetMouseButtonDown(0)){
				var hit : RaycastHit; 
				var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
				if(Physics.Raycast(ray, hit)){	
					if(hit.collider.tag == "Atom"){
						selected = hit.collider.gameObject;
						selectedOn = true;
						Debug.Log("Grabbed");
					}
				}
			}
		}
	
	if(selectedOn == true){
		selected.transform.position = V3T;
			if(Time.time > nextUse){
				if(Input.GetMouseButtonDown(0)){
					selected = null;
					selectedOn = false;
					nextUse = Time.time + delay;
					Debug.Log("dropped");
			}
		}
	}
}

I have it attached to an empty game object name MastCtrl, and the objects I’m trying to drag around are just cubes. The drop action always works, if I click while holding the object it works fine. But the actual grabbing bit sometimes needs a few extra clicks to work.

Any help would be appreciated! Thanks in advanced.

make line 26 an else statement. I’d say you hit with ifs right now, since the first enables the second by changing the bool condition.

Ok I did a little fiddling.

I got it to work much better and it works almost every time. The only time it doesn’t is if you hold the mouse button down but that’s not that big a problem!

	if(selectedOn == false){
			if(Input.GetMouseButtonDown(0)){
				var hit : RaycastHit; 
				var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
				if(Physics.Raycast(ray, hit)){	
					if(hit.collider.tag == "Atom"){
						selected = hit.collider.gameObject;
						selectedOn = true;
						Debug.Log("Grabbed");
					}
				}
			}
		}
	
	if(selectedOn == true){
		selected.transform.position = V3T;	
			if(Input.GetMouseButtonDown(0)){
				if(Time.time > nextUse){
					selected = null;
					selectedOn = false;
					nextUse = Time.time + delay;
					Debug.Log("Dropped");
			}
		}
	}
}

All I honestly did was move the

if(Time.time > delay){

below

if(Input.GetMouseButtonDown(1)){

And it seems to have worked! Hope this helps someone else.