My code launches the player based on the angle between an object and the cursor. Is there any way to also calculate the distance between the them and launch it proportionate to that distance?
‘’’
public class FirstController : MonoBehaviour
{
public GameObject Aiming;
public GameObject Player;
public float launchForce;
public float jumps = 1f;
void OnCollisionStay2D(Collision2D col)
{
if (col.gameObject.name == "Square")
{
jumps = 1f;
}
}
void Update()
{
Vector2 lastPosition = Aiming.transform.position;
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = mousePosition - lastPosition;
if (Input.GetMouseButtonDown(0))
{
Aiming.GetComponent<Transform>().position = mousePosition;
}
if (jumps > 0)
{
if (Input.GetMouseButtonUp(0))
{
transform.right = -direction;
Shoot();
jumps = jumps - 1;
}
}
}
void Shoot()
{
GetComponent<Rigidbody2D>().velocity = (transform.right * launchForce);
}
}