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.

Question Moving gameobject with mouse, issue when object is near edge of screen.

Discussion in 'Scripting' started by ReeveHere, Sep 10, 2023.

  1. ReeveHere

    ReeveHere

    Joined:
    Nov 12, 2022
    Posts:
    11
    Hello,

    I am using the following code to move a gameobject using the mouse's position:

    Code (CSharp):
    1. void update()
    2. {
    3.     selectionDistance = Vector3.Distance(ray.origin, hit.point);
    4.    
    5.     originalScreenTargetPosition = targetCamera.ScreenToWorldPoint(new             Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance));
    6.    
    7.     originalRigidbodyPos = hit.collider.transform.position;
    8. }
    9.  
    10.     private void FixedUpdate()
    11.     {
    12.         if (grabbedObject != null)
    13.         {
    14.             Vector3 mousePositionOffset = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance)) - originalScreenTargetPosition;
    15.  
    16.             rb.velocity = (originalRigidbodyPos + mousePositionOffset - rb.transform.position) * forceAmount * Time.deltaTime;
    17.         }
    18.     }
    For some reason, though, whenever the gameobject is near the left or right edge of the screen, its position becomes more and more desynced with the mouse the further it is moved. I cannot figure out why that is. Any help would be appreciated. ♥
     
  2. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    Is there a reason you are using physics to move the object? You are not going to get a smooth movement with physics and mouse position syncing.
     
  3. ReeveHere

    ReeveHere

    Joined:
    Nov 12, 2022
    Posts:
    11
    I am using physics because I want it to interact with colliders
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,251
    You can turn on interpolation for the rigidbody and you'll get smooth movement.

    @ReeveHere Firstly I would debug if the position you're calculating corresponds to where you think it is. I would perhaps use Gizmos to draw this. I would also move the rigidbody with
    Rigidbody.MovePosition
    so you can specifically set it to a position, rather than adjust its velocity.

    Also your lower case
    update
    method won't be getting called.
     
  5. ReeveHere

    ReeveHere

    Joined:
    Nov 12, 2022
    Posts:
    11
    Yeah the update thing is like that because I cut out other parts of my code which didn't relate to this problem. As for the rigidbody.moveposition, does it still interact with colliders?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,251
    It should. I believe it does a 'sweep' from it's current point and the point provided, and respects any colliders between those positions.
     
  7. ReeveHere

    ReeveHere

    Joined:
    Nov 12, 2022
    Posts:
    11
    Alright, I'll try that. Thanks!
     
  8. ReeveHere

    ReeveHere

    Joined:
    Nov 12, 2022
    Posts:
    11
    rigidbody.MovePosition is not respecting colliders. I did find out, though, that the warping of the position has something to do with the
    originalRigidbodyPos
    or the
    mousePositionOffset
     
  9. ReeveHere

    ReeveHere

    Joined:
    Nov 12, 2022
    Posts:
    11
    I got it working, this is my new code:


    Code (CSharp):
    1. void Update()
    2. {
    3.             if (Input.GetButtonDown("Fire1"))
    4.             {
    5.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    6.                 RaycastHit hit;
    7.                 if (Physics.Raycast(ray, out hit))
    8.                 {
    9.                     if (hit.collider.tag == "slimeChild" || hit.collider.tag == "crop")
    10.                     {
    11.                         if (hit.collider.tag == "slimeChild") grabbedSlime = hit.collider.transform.parent.gameObject;
    12.                         else if (hit.collider.tag == "crop") grabbedSlime = hit.collider.gameObject;
    13.                         rb = grabbedSlime.GetComponent<Rigidbody>();
    14.  
    15.                         screenPoint = Camera.main.WorldToScreenPoint(rb.transform.position);
    16.                         offset = rb.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    17.  
    18.                     }
    19.                 }
    20.             }
    21. }
    22.  
    23.     private void FixedUpdate()
    24.     {
    25.         if (grabbedSlime != null)
    26.         {
    27.             Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    28.             Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    29.  
    30.             rb.velocity = (curPosition - rb.transform.position) * forceAmount * Time.deltaTime;
    31.  
    32.         }
    33.     }
    Pro-tip: actually understanding the code helps.
     
    Last edited: Sep 12, 2023