Reputation: 55
I'm fresh in unity but for 1st project I want to make something like the messenger ball game but I have small problem with object movement. I want to make movement like this
But now I have only verticaly movement. So the question is how to in simple way change it to movement like that ball in video?
void OnMouseDown()
{
rb.velocity = Vector2.up * jumpForce;
}
Upvotes: 1
Views: 98
Reputation: 8566
The same idea as @AdamRoszyk, but there is a pre-bluit unity method for that in the RigidBody
class, named AddForceAtPosition:
void OnMouseDown()
{
var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition) ;
rb.AddForceAtPosition(Vector2.up * jumpForce, pos, ForceMode.Impulse);
}
Upvotes: 1
Reputation: 409
You need to measure the distance between center of ball and the click point. Then you can add force in the same direction you've measured. Remember to lock movements of rigid body in the z axis and constrain the ball with screen bounds (Those also can be rigidbodies- just with gravity=0).
rb.AddForce(transform.up * direction);
Link to documentation: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Upvotes: 3