Home > Blockchain >  How to move 2d Rigidbody Smoothly to Mouse on Click
How to move 2d Rigidbody Smoothly to Mouse on Click

Time:03-31

Vector2 mousePos = Input.mousePosition;
    // motion core
    if (GameObject.Find("Camera").GetComponent<room>().playerNum == 1)
    {
        if (Input.GetMouseButtonDown(0))
        {
            // move script not working
        }

    }

So, I have mostly every possible solution I could find but none of them worked. I cannot get it to smoothly move with AddForce because I could not figure out a working algorithm to make the force move toward the MousePosition.

CodePudding user response:

The position you're getting out of Input.mousePosition are the coordinates on the screen, not the position in the world. To transform between the two, you can use Camera.ScreenToWorldPoint().

if (Input.GetMouseButtonDown(0))
    {
        Vector3 mousePos = Input.mousePosition
        mousePos = new Vector3(mousePos.x, mousePos.y, Camera.main.nearClipPlane)
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3())

        // move script
    }

You might need to edit the z coordinate in the mousePos to the current transform.position.z of the object you're trying to move, or another value that makes most sense here. It acts as a kind of wall, where it'll create the point exactly that far from the camera on your mouse position. This should be a lot cheaper than raycasting, and still works if there's nothing to hit where you're clicking.

CodePudding user response:

I have a script that did this same movement to the handle of a wrecking ball, but I don't have the code with me at the moment. I can't remember exactly how it worked, but I think the idea was that when the mouse was clicked, drag would be set to a very high number and gravity would be set to 0. Then an extremely strong force would be added to counter the drag so that the object would fly towards the mouse without orbiting. When the mouse was released, the drag and gravity would be set back to normal.

I can't test this at the moment because I'm on a chromebook and my PC with Unity on it is in another building, but this code should do the trick if I don't make any errors.

using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    float prevDrag, prevGrav;    

    bool mousedown;
    Plane plane;
    Rigidbody2D r;
    
    void Start()
    {
        r = GetComponent<Rigidbody2D>(); // assuming this script is attached to the object being moved.
        plane = new Plane(Vector3.up, Vector3.zero);
    }

    void Update()
    {
        if(mousedown)
        {
            float enter;
            if (plane.Raycast(ray, out enter))
            {
                var hitPoint = ray.GetPoint(enter);               
                var mouseDir = hitPoint - gameObject.transform.position;   
                rb.AddForce(mouseDir * 9999999);
            }
        }
    }
    
    void onm ouseDown()
    {
        mousedown = true;
        prevDrag = r.drag;
        prevGrav = r.gravity;
        r.drag = 99999;
        r.gravity = 0;
    }

    void onm ouseUp()
    {
        mousedown = false;
        r.drag = prevDrag;
        r.gravity = prevGrav;
    }
}
  • Related