Weird instantaneous teleporting object

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

Hey man, I am no expert myself but try moving the “Vector3 mouseposition” (line 73) straight under Update, It may have something to do with mouseposition being set on that same frame you moving the furniture.

1 Like

does indeed sound like it’s being set to Vector3.zero, which is the default “I don’t have a value” value for a vector3 for a frame. I assume “center of the room” is at the origin of the scene.

I tried moving the line 73 to update but it doesn’t really change anything. Still does the same teleport bug.

You seem to be setting the position differently when you pick up the object, you’re doing this:

Vector3 mouseposition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
currentfurniture.position = new Vector3(mouseposition.x, 0, mouseposition.z);

But then when you move it around, you are doing this:

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);

Maybe try adding the z component in ScreenToWorldPoint when you are picking it up as well.

Sorry for late reply, steego you were right. I just simply replaced the pick up object code with the move it around code. Now works perfect, don’t remember why I even bothered changing the code in the first place.