- Home /
Object follows mouse onclick
So I'm trying to get an object to follow my mouse position after I have clicked on it. Currently it moves only when I click within the object's collider and it's an instantaneous teleportation not a mouse follow. I want to make it follow the mouse.
Edit: Just to make it clearer, player clicks object, object sticks to cursor, player moves cursor and object follows then player clicks within buildable location and object will stick to that location until player clicks it again.
Edit2: Added some pieces of code I forgot to put in.
if (hasplaced && Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
if (hit.transform.tag == "GroundFurniture")
{currentfurniture = hit.transform;
if(Input.GetMouseButtonDown(0))
{
hasplaced = false;
Vector3 mouseposition = Input.mousePosition;
currentfurniture.transform.position = new Vector3(mouseposition.x, 0, mouseposition.z);
}
Debug.Log(currentfurniture);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Y position keeps changing, after action is complete? 1 Answer
Any way to change transform.position of an indefinite number of gameobjects on FixedUpdate? 2 Answers
Subtracting the position of transform from the position of Game Objects in a list. 1 Answer