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

Hello,

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

void update()
{
    selectionDistance = Vector3.Distance(ray.origin, hit.point);

    originalScreenTargetPosition = targetCamera.ScreenToWorldPoint(new             Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance));

    originalRigidbodyPos = hit.collider.transform.position;
}

    private void FixedUpdate()
    {
        if (grabbedObject != null)
        {
            Vector3 mousePositionOffset = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance)) - originalScreenTargetPosition;

            rb.velocity = (originalRigidbodyPos + mousePositionOffset - rb.transform.position) * forceAmount * Time.deltaTime;
        }
    }

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. ♥

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.


I am using physics because I want it to interact with colliders


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.


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?

It should. I believe it does a ‘sweep’ from it’s current point and the point provided, and respects any colliders between those positions.


Alright, I'll try that. Thanks!


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

I got it working, this is my new code:

void Update()
{
            if (Input.GetButtonDown("Fire1"))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.collider.tag == "slimeChild" || hit.collider.tag == "crop")
                    {
                        if (hit.collider.tag == "slimeChild") grabbedSlime = hit.collider.transform.parent.gameObject;
                        else if (hit.collider.tag == "crop") grabbedSlime = hit.collider.gameObject;
                        rb = grabbedSlime.GetComponent<Rigidbody>();

                        screenPoint = Camera.main.WorldToScreenPoint(rb.transform.position);
                        offset = rb.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

                    }
                }
            }
}

    private void FixedUpdate()
    {
        if (grabbedSlime != null)
        {
            Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
            Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;

            rb.velocity = (curPosition - rb.transform.position) * forceAmount * Time.deltaTime;

        }
    }

Pro-tip: actually understanding the code helps.