Mouse-click, Drag, Launch physics object

Alright guys, I’m brand new to JS and CS but I’m trying to script this functionality: I want to click my mouse button down on a ball, pull the mouse back and when I release the button, the ball launches in the opposite direction (X,Z only)with force based on the distance I pulled the mouse back while held. I followed a tutorial for controlling the ball with keys and had it working but it’s not what I need.

To me it’s three things: 1) Determining the position of the ball when mouse button is pressed 2) Determining the distance and direction the cursor has been moved on mouse button release 3) Applying the distance and direction * multiplier (with a maximum) as force in the opposite direction.

I’m not looking for anyone to write the code for me, I’m having fun learning it, I just need to be mentored a little bit. Thanks!

Here’s where I’ve gotten through some tutorials and guessing:

	void Update () 
	{

		if (Input.GetMouseButton(0)) 
		{
		rigidbody.AddForce(Vector3.forward * 10);
		}
	}
}

I also realize this script doesn’t only work if you mouse over the ball, it happens anywhere. Ideally I’d like it to only work if the mouse is over the ball.

-Bryan

Here are some suggestions on things to look into for your goal:

  • Take a look at OnMouseDown() and OnMouseDrag() and OnMouseUp(). These functions will only be called if you click on the object.

  • If you save the position of the object when you start to drag, you can create the force vector when in OnMouseUp(). Something like:

    rigidbody.AddForce((savedPos - transform.position) * 500);

  • When dragging you are going to have to worry about converting screen coordinates into world coordinates. See Camera.ScreenToWorldPoint().

Thank you very much. So far I’ve gotten the mouse down and mouse up positions to read to debug. Now I need to subtract those numbers and create a force to launch the ball in the opposite direction. I looked at the Camera.ScreenToWorldPoint() but I’m not using the camera to tell distance, I want the distance to be the difference between mousedown and mouse up on a “top-down” 2D plane (so pulling left makes it go right, down goes up, etc). Here’s where I’m at:

Vector3 mMouseDownPos;
Vector3 mMouseUpPos;

public float speed = .1f;

// Use this for initialization
void Start () 
{

}

// Update is called once per frame
void Update () 
{

}

void OnMouseDown() 
{
	mMouseDownPos = Input.mousePosition;
	Debug.Log( "the mouse down pos is " + mMouseDownPos.y.ToString() );
	mMouseDownPos = Input.mousePosition;
	Debug.Log( "the mouse down pos is " + mMouseDownPos.z.ToString() );
	mMouseDownPos.z = 0;
}

void OnMouseUp() 
{
	mMouseUpPos = Input.mousePosition;
	mMouseUpPos = Input.mousePosition;
	mMouseUpPos.z = 0;
	var direction = mMouseDownPos - mMouseUpPos;
	direction.Normalize();
	rigidbody.AddForce (direction * speed);
	Debug.Log( "the mouse up pos is " + mMouseUpPos.ToString() );
}

}

This is what you are looking for

Unity force Metter - Pull release add Unity force Metter - Pull release add force - YouTube