Hello, im trying to limit float values based off a drag starting from “minforce” to “maxforce”. But this code is getting the exact value of the drag that can be too much force or too little force. help is needed >.<
public Rigidbody2D rb;
Vector3 startpos, dragpos, endpos, dir;
public float minforce, maxforce;
void Update()
{
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
Vector3 mousePosition = Input.mousePosition;
startpos = Camera.main.ScreenToWorldPoint(mousePosition);
startpos.z = 0;
}
if (touch.phase == TouchPhase.Moved)
{
Vector3 mousePosition = Input.mousePosition;
dragpos = Camera.main.ScreenToWorldPoint(mousePosition);
dragpos.z = 0;
}
if (touch.phase == TouchPhase.Ended)
{
endpos = Camera.main.ScreenToWorldPoint(touch.position);
dir = startpos - endpos;
Vector2 clamp = Vector2.ClampMagnitude(dir, maxforce) * minforce;
rb.AddForce(clamp, ForceMode2D.Impulse);
}
}
}