- Home /
Follow mouse until mouseclick
I have a function call MoveObject() that requires a gameobject to be passed to it.
The objective is that any gameobject passed to the function will follow the mouse around until the user clicks the left mouse button where it will be frozen into place.
theres two functions AddObject() instantiates an object based on the object index that is passed to it. Once the AddObject function instantiates the object, that object gets passed onto MoveObject()
From there the object should move with the mouse cursur until the user clicks the left mouse button.
However thats not what Im getting. Ive been looking over this code for hours and cant figure out the problem, any help is appreciated.
//Instantiate Objects from the building pallete public void AddObject(int index) { var typeofbuilding = buildingTypes[index];
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 1000))
{
Instantiate(typeofbuilding, hit.point, Quaternion.identity);
MoveObject(typeofbuilding);
return;
}
}
//move the selected object around public void MoveObject(GameObject movingBuilding) { //Any object passed to this function should follow the mouse around var canmove = true; //as long as this value is true, loop through the function if(canmove == true) { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); RaycastHit hit; if (Physics.Raycast (ray, out hit, 1000)) { if(Input.GetButtonDown("LMB")) { movingBuilding = null; //release the gameobject from the movingobject variable canmove = false; } else { movingBuilding.transform.position = hit.point; //as long as the left mouse button isnt pushed follow the mouse cursor } } }
}