Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Weird instantaneous teleporting object

Discussion in 'Scripting' started by kityanlam3, May 12, 2016.

  1. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41
    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.



    Code (CSharp):
    1. void Update()
    2.     {
    3.         Debug.Log(HoldingObject);
    4.         //Start of groundedfurniture code
    5.         //if there is a furniture selected and it is ground furniture run code
    6.         if (currentfurniture != null && currentfurniture.gameObject.tag == "GroundFurniture")
    7.         {
    8.             //if haven't placed just move it around
    9.             if (HoldingObject)
    10.             {
    11.                 //Don't let other code run while player moves the object around
    12.                 nowyoucanrun = false;
    13.                 Vector3 m = Input.mousePosition;
    14.                 m = new Vector3(m.x, m.y, transform.position.y);
    15.                 Vector3 p = Camera.main.ScreenToWorldPoint(m);
    16.                 currentfurniture.position = new Vector3(p.x, 0, p.z);
    17.                 //Rotate by scrolling
    18.                 if (Input.GetAxis("Mouse ScrollWheel") > 0)
    19.                 {
    20.                     currentfurniture.Rotate(0, Input.GetAxis("Mouse ScrollWheel") * speed, 0);
    21.  
    22.                 }
    23.                 else if (Input.GetAxis("Mouse ScrollWheel") < 0)
    24.                 {
    25.                     currentfurniture.Rotate(0, -Input.GetAxis("Mouse ScrollWheel") * -speed, 0);
    26.                 }
    27.                 //end of rotate by scrolling
    28.                
    29.                 //if right mouse click then destroy the current gameobject and allow player to select another
    30.                 if (Input.GetMouseButtonDown(1))
    31.                 {
    32.                     Destroy(currentfurniture.gameObject);
    33.                     HoldingObject = false;
    34.                 }
    35.                 //end
    36.                 //if click fire raycast on the ground
    37.                 if (Input.GetMouseButtonDown(0))
    38.                 {
    39.                     camera.cameraFreezerot = false;
    40.                     RaycastHit hit;
    41.                     Physics.Raycast(currentfurniture.position, Vector3.down, out hit);
    42.                     // if it's not in the area don't place
    43.                     if (hit.transform.tag != "BuildableArea")
    44.                     {
    45.                         HoldingObject = true;
    46.  
    47.                     }
    48.                     else // if it is in the area place it and change bool flags.
    49.                     {
    50.                         HoldingObject = false;
    51.                         nowyoucanrun = true;
    52.                         currentfurniture.position = new Vector3(p.x, hit.point.y, p.z);
    53.                        
    54.                     }
    55.                 }
    56.             }
    57.                 //If player is not holding onto an object run this code
    58.             else if (!HoldingObject)
    59.             {
    60.                 //If allowed to run this code and player has clicked the button then run it.
    61.                 if (Input.GetMouseButtonDown(0) && nowyoucanrun == true)
    62.                 {
    63.                     //Hold to fire raycast at mouse position
    64.                         RaycastHit hit;
    65.                         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    66.                         Physics.Raycast(ray, out hit);
    67.                         //If hits object with tag ground furniture, object follows mouse
    68.                         if (hit.transform.tag == "GroundFurniture")
    69.                         {
    70.                             camera.cameraFreezerot = true;
    71.                             currentfurniture = hit.transform;
    72.                             HoldingObject = true;
    73.                             Vector3 mouseposition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    74.                             currentfurniture.position = new Vector3(mouseposition.x, 0, mouseposition.z);
    75.                             Debug.Log("It's ground furniture");
    76.                         }
    77.                    
    78.                 }
    79.             }
    80.         }
    81.        
    82.        
    83.  
    84.         //End of grounded furniture code
     
  2. BrendanKZN

    BrendanKZN

    Joined:
    Jun 22, 2011
    Posts:
    157
    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.
     
    LeftyRighty likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
  4. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41
    I tried moving the line 73 to update but it doesn't really change anything. Still does the same teleport bug.
     
  5. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    967
    You seem to be setting the position differently when you pick up the object, you're doing this:

    Code (csharp):
    1.  
    2. Vector3 mouseposition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    3. currentfurniture.position = new Vector3(mouseposition.x, 0, mouseposition.z);
    4.  
    But then when you move it around, you are doing this:

    Code (csharp):
    1.  
    2. Vector3 m = Input.mousePosition;
    3. m = new Vector3(m.x, m.y, transform.position.y);
    4. Vector3 p = Camera.main.ScreenToWorldPoint(m);
    5. currentfurniture.position = new Vector3(p.x, 0, p.z);
    6.  
    Maybe try adding the z component in ScreenToWorldPoint when you are picking it up as well.
     
  6. kityanlam3

    kityanlam3

    Joined:
    Feb 2, 2016
    Posts:
    41
    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.