Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

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

$
0
0
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.

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>