How to throw an object while swiping and after
Hello, I'm trying to create a game where player has to grab an object and swipe it to change it's position. It should look exactly like in this Youtube Video:
I was trying many scripts and methodds but never achieved the same resultat. Anyone can help? Thank ou very much. My current code is this, but still it's not it:
Camera camera;
Vector3 posWhileMovement, startPos, endPos, direction; // touch start position, touch end position, swipe direction
float touchTimeStart, touchTimeFinish, timeInterval; // to calculate swipe time
[Range(0.05f, 1f)] // slider for inspector window
public float throwForse = 0.3f; // to control throw forse
void Start()
{
camera = Camera.main;
}
// Update is called once per frame
void Update()
{
// if you touch the screen
if (Input.GetMouseButtonDown(0))
{
// getting touch position and marking time when you touch the screen
touchTimeStart = Time.time;
startPos = camera.ScreenToWorldPoint(Input.mousePosition);
}
if (Input.GetMouseButton(0))
{
//posWhileMovement = new Vector3(camera.ScreenToWorldPoint(Input.mousePosition).x, camera.ScreenToWorldPoint(Input.mousePosition).y, 0f);
//transform.DOMove(posWhileMovement, 0.1f);
}
// if you release your finger
if (Input.GetMouseButtonUp(0))
{
// marking time when you release it
touchTimeFinish = Time.time;
// calculate swipe time interval
timeInterval = touchTimeFinish - touchTimeStart;
// getting release finger position
endPos = new Vector3(camera.ScreenToWorldPoint(Input.mousePosition).x, camera.ScreenToWorldPoint(Input.mousePosition).y, 0f);
// calculating swipe direction
direction = startPos - endPos;
GetComponent<Rigidbody2D>().AddForce(-direction/timeInterval * throwForse);
}
}
}
Comment