- Home /
"Drag" gameobject around environment with mouse
So I was messing around trying to find a way to drag objects around the game environment with the mouse. I already have a real basic system in place to move character to a point that is clicked (using a raycast). I am trying to expand that basic model to take some different actions based on what object that ray hits. This is what I am woking with at the moment.
void Update () {
//When the Mouse is initially clicked do this
if (Input.GetButtonDown("primaryMouse") && shootMouseRay(camRay, out rayHit)) {
debugMi.text = rayHit.point.ToString(); //debug output
//Check to see if this is an interaction object, if it is inspect the object
if (rayHit.collider.gameObject.layer == interactionLayer) {
switch (rayHit.collider.gameObject.tag) {
case "Enemy" :
target = rayHit.collider.gameObject;
navAgent.MoveToGameObject(target, replan);
debugT.text = rayHit.collider.gameObject.ToString(); //debug output
break;
case "drag" :
grabThing(rayHit.transform, camRay, rayHit);
break;
}
} else {
target = null;
debugT.text = ""; //debug output
}
}
//When the Mouse is held do this if not in dragobj mode
if (!dragMode && Input.GetButton("primaryMouse") && shootMouseRay(camRay, out rayHit)) {
navAgent.MoveToPosition(rayHit.point, replan);
debugMc.text = rayHit.point.ToString();
}
}
//Method to shoot a ray from the camera "at" the mouse pointer
bool shootMouseRay (Ray ray, out RaycastHit hit) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
return Physics.Raycast(ray, out hit);
}
//Grab an object and drag it around
void grabThing (Transform objpos, Ray ray, RaycastHit hit) {
if (Input.GetButton("primaryMouse")) {
dragMode = true;
if (shootMouseRay(ray, out hit)) {
objpos.position = new Vector3(hit.point.x, objpos.position.y, hit.point.z);
}
}
if (Input.GetButtonUp("primaryMouse")) {
dragMode = false;
}
}
Everything works pretty close to what I expect except for the dragging function. I have tried rewriting it a few ways. Basically I want to disable movement functions while dragging stuff and enter into my drag mode as long as a user holds a button down on the drag able gameObject. When the button is released I wanted to set my dragmode bool to false. This isn't what's occurring now.
When I click on any dragable object in my scene it will ever so slightly move the object over but it does not continue in dragmode as long as the mouse button is held down. Does this have to do with splitting it off into a separate method outside of update? Thanks a lot for any help!
Answer by Mortoc · Jun 11, 2012 at 01:54 AM
It sort of has to do with the external function. Generally you want to split things up in to functions exactly like you did, but I think it made you lose track of your logic flow.
The switch statement that's in Update is only hit on the frames where MouseButtonDown is true. When your function is called after that, GetButton will return true for that frame and GetButtonUp will never be hit.
For this problem (and any problem like it), throw in a bunch of Debug.Logs to see where the program flow goes to.
So if I have something like
if (Input.GetButtonDown("mouse1")) { code }
all of the code in that case is scoped to only the first frame even if I nest some kind of general "GetButton" in there to check if it's being held down after the initial click was over a select able object?
Yes, GetButtonDown only returns true the frame the button went down. If you want a function that returns true every frame the button is held down, you want to use Input.GetButton