using UnityEngine;
public class BallController : MonoBehaviour
{
private Rigidbody2D rb;
private bool isDragging = false;
private Vector3 startPoint;
private Vector3 endPoint;
private Vector2 direction;
private float forceMagnitude = 10f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
startPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
isDragging = true;
}
if (Input.GetMouseButtonUp(0) && isDragging)
{
endPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = (endPoint - startPoint).normalized;
rb.AddForce(direction * forceMagnitude, ForceMode2D.Impulse);
isDragging = false;
}
if (isDragging)
{
Vector3 currentPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(currentPoint.x, currentPoint.y, transform.position.z);
}
}
}