Moving GameObject with Add force and mouse drag

Imgur

The picture above is what I am trying to accomplish. I want the player to be able to drag their finger and move the object around. Once the finger is removed I would like the object to keep moving. How would I do something like this? I don’t know what is involved so I don’t really know where to start. Even just a list of things to do with out code would be helpful because I don’t know where to start.

I think you need to do 2 main things:

  1. Checking the drag and moving objects with the finger
  2. Check if finger is up and adding the Force to the GameObject.

May be your simplest solution’s Update() can look like this:

        bool _touchBegan = false;
        private void Update()
        {
            if (Input.touchCount <= 0)
            {
                return;
            }

            Touch currentTouch = Input.GetTouch(0);
            Vector2 currentTouchPosition = currentTouch.position;
            Vector3 touchPointInWorld = Camera.main.ScreenToWorldPoint(new Vector3(currentTouchPosition.x, currentTouchPosition.y));
            
            if (currentTouch.phase == TouchPhase.Began && _spriteRenderer.bounds.Contains(touchPointInWorld) && !_touchBegan)
            {
                _touchBegan = true;
                return;
            }
           
            if (currentTouch.phase == TouchPhase.Moved && _touchBegan)
            {
                transform.localPosition = Camera.main.ScreenToWorldPoint(new Vector3(currentTouchPosition.x, currentTouchPosition.y));
                return;
            }
            
            if (currentTouch.phase == TouchPhase.Ended || currentTouch.phase == TouchPhase.Canceled && _touchBegan)
            {
                //AddForce
                _touchBegan = false;
                return;
            }
        }

So you should to attach this script to the object which you want to drag.

_spriteRenderer.bounds.Contains(touchPointInWorld) - checking if the touch’s point in inside the bounds of you gameObject (in my case it is gameObject with SpriteRenderer, i.e. Unity 2d sprite).

//AddForce - this means that in this moment finger is Up and you may to add force by rigidbody.AddForce(…) or in the other way.

So it is the simplest solution, I hope this lines are readable and I hope they will help you:)

So I feel like I noted it pretty well. It was just as easy as just setting the gameObjest.rigidBody.velocity to the force that is found through the two points of the mouse.

    public Vector3 gameObjectSreenPoint;
    public Vector3 mousePreviousLocation;
    public Vector3 mouseCurLocation;
    void OnMouseDown()
    {
        //This grabs the position of the object in the world and turns it into the position on the screen
        gameObjectSreenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
        //Sets the mouse pointers vector3
        mousePreviousLocation = new Vector3(Input.mousePosition.x, Input.mousePosition.y, gameObjectSreenPoint.z);
    }

    public Vector3 force;
    public Vector3 objectCurrentPosition;
    public Vector3 objectTargetPosition;
    public float topSpeed = 10;
    void OnMouseDrag()
    {
        mouseCurLocation = new Vector3(Input.mousePosition.x, Input.mousePosition.y, gameObjectSreenPoint.z);
        force = mouseCurLocation - mousePreviousLocation;//Changes the force to be applied
        mousePreviousLocation = mouseCurLocation;
    }

    public void OnMouseUp()
    {
        //Makes sure there isn't a ludicrous speed
        if (rigidbody.velocity.magnitude > topSpeed)
            force = rigidbody.velocity.normalized * topSpeed;
    }

    public void FixedUpdate()
    {
        rigidbody.velocity = force;
    }