So this code is meant to grab an instantiated object and move it around then clicking will let it be placed. Then once it’s placed, player can then pick it up with another left click and then drop with left click wherever he wants.
It’s working but after the initial placement of the object, when player picks up the placed object the object will for a split second go to the center of the room then follow the mouse cursor. Not sure which part of the code is causing it and I’ve been running through it a few times.
Summary: click n drag function. if pick up object 2nd time will have instant teleport to center then teleport back to mouse then work as intended.
void Update()
{
Debug.Log(HoldingObject);
//Start of groundedfurniture code
//if there is a furniture selected and it is ground furniture run code
if (currentfurniture != null && currentfurniture.gameObject.tag == "GroundFurniture")
{
//if haven't placed just move it around
if (HoldingObject)
{
//Don't let other code run while player moves the object around
nowyoucanrun = false;
Vector3 m = Input.mousePosition;
m = new Vector3(m.x, m.y, transform.position.y);
Vector3 p = Camera.main.ScreenToWorldPoint(m);
currentfurniture.position = new Vector3(p.x, 0, p.z);
//Rotate by scrolling
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
currentfurniture.Rotate(0, Input.GetAxis("Mouse ScrollWheel") * speed, 0);
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
currentfurniture.Rotate(0, -Input.GetAxis("Mouse ScrollWheel") * -speed, 0);
}
//end of rotate by scrolling
//if right mouse click then destroy the current gameobject and allow player to select another
if (Input.GetMouseButtonDown(1))
{
Destroy(currentfurniture.gameObject);
HoldingObject = false;
}
//end
//if click fire raycast on the ground
if (Input.GetMouseButtonDown(0))
{
camera.cameraFreezerot = false;
RaycastHit hit;
Physics.Raycast(currentfurniture.position, Vector3.down, out hit);
// if it's not in the area don't place
if (hit.transform.tag != "BuildableArea")
{
HoldingObject = true;
}
else // if it is in the area place it and change bool flags.
{
HoldingObject = false;
nowyoucanrun = true;
currentfurniture.position = new Vector3(p.x, hit.point.y, p.z);
}
}
}
//If player is not holding onto an object run this code
else if (!HoldingObject)
{
//If allowed to run this code and player has clicked the button then run it.
if (Input.GetMouseButtonDown(0) && nowyoucanrun == true)
{
//Hold to fire raycast at mouse position
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
//If hits object with tag ground furniture, object follows mouse
if (hit.transform.tag == "GroundFurniture")
{
camera.cameraFreezerot = true;
currentfurniture = hit.transform;
HoldingObject = true;
Vector3 mouseposition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
currentfurniture.position = new Vector3(mouseposition.x, 0, mouseposition.z);
Debug.Log("It's ground furniture");
}
}
}
}
//End of grounded furniture code