here is the code.
private Vector2 lastTouchPos;
[HideInInspector]
public bool nonstop;
private Rigidbody rigidbody;
private Vector3 startpos;
private Vector3 endpos;
private Vector3 final;
Vector2 touchDeltaPosition;
Vector2 touchPosition;
public GameObject scorePanel;
private OpenSocreScript openSocreScript;
public GameObject ball;
private void Start()
{
nonstop = true;
rigidbody = GetComponent<Rigidbody>();
openSocreScript = scorePanel.GetComponent<OpenSocreScript>();
}
private void FixedUpdate()
{
// transform.position = new Vector3(Mathf.Clamp(transform.position.x, -2.4f, 2.4f), transform.position.y, transform.position.z);
if (Input.touchCount > 0 && nonstop)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
final = Vector3.zero;
touchDeltaPosition = Input.GetTouch(0).position;
startpos = new Vector3(touchDeltaPosition.x, 0, touchDeltaPosition.y);
}
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 v = Input.GetTouch(0).deltaPosition;
// lastTouchPos = Input.GetTouch(0).position;
// transform.Translate(new Vector3(v.x * Time.fixedDeltaTime * 2, 0, 0) , Space.World);
rigidbody.velocity = new Vector3(v.x * Time.fixedDeltaTime*2, 0, 0);
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
touchPosition = Input.GetTouch(0).position;
endpos = new Vector3(touchPosition.x, 0, touchPosition.y);
final = endpos - startpos;
Debug.Log("touch offset............" + final.normalized);
if (touchPosition.y - touchDeltaPosition.y > 200f)
{
// openSocreScript.OpenMenu();
rigidbody.AddForce(final.normalized * 10f * Time.fixedDeltaTime*100, ForceMode.Impulse);
nonstop = false;
AnimateBall();
}
}
}
}
public void AnimateBall()
{
if (ball != null)
{
Animator animator = ball.GetComponent<Animator>();
if (animator != null)
{
bool isOpen = animator.GetBool("open");
animator.SetBool("open", !isOpen);
}
}
}
public void Reset(object _ball)
{
AnimateBall();
gameObject.transform.position = new Vector3(0f, 0.4451873f, -7.29f);
gameObject.transform.rotation = Quaternion.Euler(new Vector3 (0,0,0));
gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
gameObject.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
}