У меня небольшая проблема с кодом.
public Transform Nut;
public Transform cannonPoint;
public bool canShoot = true;
Rigidbody2D nutRb;
public float forceVelocity;
public float maxTop, minBottom;
private void Start()
{
nutRb = Nut.GetComponent<Rigidbody2D>();
Nut.gameObject.AddComponent<ColliderEventSystem>().TriggerEntered += NutResset;
}
private void NutResset(ColliderEventSystem eventTarget, Collider2D other)
{
Nut.SetParent(transform);
Nut.localPosition = cannonPoint.localPosition;
nutRb.simulated = false;
nutRb.velocity = Vector3.zero;
canShoot = true;
}
void Update()
{
var pos = Camera.main.WorldToScreenPoint(transform.position);
var dir = Input.mousePosition - pos;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
if (angle >= minBottom && angle <= maxTop)
{
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
if (Input.GetMouseButtonDown(0) && canShoot)
LaunchNut(dir);
}
}
private void LaunchNut(Vector3 launchPoint)
{
Nut.SetParent(null);
canShoot = false;
nutRb.simulated = true;
Geom.launchObj(Nut.gameObject, cannonPoint.position, launchPoint, forceVelocity * 100);
}
Код работает нормально, но я хочу, чтобы forceVelocity менялась в зависимости от расстояния между cannonPoint и положением мыши. Например, если положение мыши выше, то forceVelocity выше, поэтому, если положение мыши меньше, то forceVelocity меньше.
Большое спасибо !!!