Making a throwable object with the distance based on the dragging force

First of all, the game scenery would be in 2D, so it can only allow objects to be thrown on the X axis. What I am hoping to achieve is;

To make a part move based on how much force the player has applied to the dragging. Click and hold the part, move it back and then throw it forward.

1 Like

I have managed to make the object follow the mouse with this code;
if (Input.GetMouseButtonDown(0))
{
if (gun.GetComponent<GunPaint>().isGun == false)
{
gun.GetComponent<GunPaint>().isGun = true;
gun.GetComponent<GunPaint>().gunDistance = Vector3.Distance(gun.transform.position, transform.position);
}
transform.position = new Vector3(transform.position.x, transform.position.y, gun.transform.position.z + gun.GetComponent<GunPaint>().gunDistance);
}
if (Input.GetMouseButton(0))
{
if (gun.GetComponent<GunPaint>().isGun == true)
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(mousePos.x, transform.position.y, transform.position.z);
}
}

Basically, I click on the object and it calculate the distance between the object and the gun, and then it follows the mouse.
Now I am a little bit lost on how to make it move at the speed of how long I have dragged it back. I have tried to make it move with addforce, but it just moves back and does not follow the mouse.
Is there anyone who could give me a helping hand? I would appreciate it :smiley:

There’s a lot of ways you could do this. Here are some ideas:

You could hold the object at a constant speed while dragging and then let go, like a slingshot. The longer you drag, the faster it goes when you let go. Be sure to check that you’re only dragging on one axis (x) and not on the y as well. In that case you could store the velocity of the drag as it happens, and then apply it to the object when you let go.
You could make the object accelerate as it’s being dragged back, the farther back you drag it, the faster it’ll accelerate. This would probably be a lot more work

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.