Hi, I have a script which add force to a Ball when we drag Mouse OR Touch on screen just like angry bird. But when I drag mouse on screen force is added to ball which is fine. But when we drag again even if ball is in air it force is added again. I want to add force just one time. How can i disable force if it is already added? Check my script codes.
using UnityEngine;
public class GameManager : MonoBehaviour
{
#region Singleton class: GameManager
public static GameManager Instance;
void Awake ()
{
if (Instance == null) {
Instance = this;
}
}
#endregion
Camera cam;
public Ball ball;
public Trajectory trajectory;
[SerializeField] float pushForce = 4f;
bool isDragging = false;
Vector2 startPoint;
Vector2 endPoint;
Vector2 direction;
Vector2 force;
float distance;
//---------------------------------------
void Start ()
{
cam = Camera.main;
ball.DesactivateRb ();
}
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
isDragging = true;
OnDragStart ();
}
if (Input.GetMouseButtonUp (0)) {
isDragging = false;
OnDragEnd ();
}
if (isDragging) {
OnDrag ();
}
}
//-Drag--------------------------------------
void OnDragStart ()
{
ball.DesactivateRb ();
startPoint = cam.ScreenToWorldPoint (Input.mousePosition);
trajectory.Show ();
}
void OnDrag ()
{
endPoint = cam.ScreenToWorldPoint (Input.mousePosition);
distance = Vector2.Distance (startPoint, endPoint);
direction = (startPoint - endPoint).normalized;
force = direction * distance * pushForce;
//just for debug
Debug.DrawLine (startPoint, endPoint);
trajectory.UpdateDots (ball.pos, force);
}
void OnDragEnd ()
{
//push the ball
ball.ActivateRb ();
ball.Push (force);
trajectory.Hide ();
}
}