How to Swipe a GameObject(Ball) in c# Unity3d,so that it will move in any direction.I am writing a script in c#.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Swipeball : MonoBehaviour {
Vector2 startPos, endPos, direcion;
float touchTimeStart, touchTimeEnd, timeIntervl;
float throwForceInXandY = 0.5f, throwForceInZ = 100f;
Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) // if (Input.touchCount>0 && Input.GetTouch(0).phase==TouchPhase.Began)
{
touchTimeStart = Time.time;
startPos = Input.mousePosition; //Input.GetTouch(0).position;
}
if (Input.GetMouseButtonUp(0))// if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
touchTimeEnd = Time.time;
timeIntervl = touchTimeEnd - touchTimeStart;
endPos = Input.mousePosition; //Input.GetTouch(0).position;
direcion = endPos - startPos;
rb.isKinematic = false;
rb.AddForce(direcion.x * throwForceInXandY, direcion.y * throwForceInXandY, throwForceInZ / timeIntervl);
// Destroy(gameObject, 3f);
}
}
}
Hello,
how can i calculate the throwforce, depending off the speed from the swipe?
Many thanks