- Home /
how to flick ball in the finger direction
using TouchScript.Events; using TouchScript.Gestures; using UnityEngine;
public class FlickExample_Rotator : MonoBehaviour {
public Rigidbody Target;
private Quaternion targetRotation;
private Vector3 speed;
public bool test;
private void Start()
{
GetComponent<FlickGesture>().StateChanged += flickHandler;
GetComponent<PressGesture>().StateChanged += pressHandler;
Target.transform.eulerAngles = new Vector3(0.0f, 0f, 0.0f);
Target.AddTorque(0,0,0);
}
private void pressHandler(object sender, GestureStateChangeEventArgs gestureStateChangeEventArgs)
{
// Target.angularVelocity = Vector3.forward;
}
void Update()
{
//SendMessage("Follow",SendMessageOptions.DontRequireReceiver);
Target = GameObject.FindGameObjectWithTag("Football").rigidbody;
}
private void FixedUpdate()
{
if (speed != Vector3.zero)
{
Target.AddForce(speed);
Target.AddTorque(5,5,5);
speed = Vector2.zero;
}
}
public void flickHandler(object sender, GestureStateChangeEventArgs e)
{
//if(test== true)
{
if (e.State == Gesture.GestureState.Recognized)
{
var spd = ((sender as FlickGesture).ScreenFlickVector/(sender as FlickGesture).ScreenFlickTime);
speed = new Vector3(Mathf.Clamp(spd.x,-500,500),Mathf.Clamp(spd.y,-450,450),-((camera.nearClipPlane - 1000)));
SendMessage("DontFollow",SendMessageOptions.DontRequireReceiver);
spd = speed;
}
}
}
void OnGUI()
{
if(GUI.Button(new Rect(0,0,100,50),"Reload"))
{
Application.LoadLevel(Application.loadedLevel);
}
}
}
[1]: /storage/temp/23461-flick.txt
I am using this script which is flicking the ball. but it is not flicking the ball properly in the direction of finger..
please edit your question and paste the code into it. then select the code and press the "code sample" button (with 01010 icon...)
Perhaps
speed = new Vector3($$anonymous$$athf.Clamp(spd.x,-500,500),$$anonymous$$athf.Clamp(spd.y,-450,450),-((camera.nearClipPlane - 1000)));
doesn't calculate the direction correctly?
@shadox: i am having this script which is also flicking ball.`#pragma strict var factor = 100.0;
private var startTime : float; private var startPos : Vector3; var cam : GameObject; var target : Rigidbody;
function Awake() { gameObject.GetComponent(testing).enabled = true; } function Update() { cam = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera"); target = GameObject.FindGameObjectWithTag("Football").rigidbody;
} function On$$anonymous$$ouseDown() { startTime = Time.time; startPos = Input.mousePosition; startPos.z = transform.position.z - Camera.main.transform.position.z; startPos = Camera.main.ScreenToWorldPoint(startPos);
}
function On$$anonymous$$ouseUp() { var endPos = Input.mousePosition; endPos.z = transform.position.z - Camera.main.transform.position.z; var temp : Vector3 = transform.position; temp.y = $$anonymous$$athf.Clamp(0,temp.y *.5f,0); transform.position = temp; endPos = Camera.main.ScreenToWorldPoint(endPos); cam.Send$$anonymous$$essage("DontFollow",Send$$anonymous$$essageOptions.DontRequireReceiver);
var force = (endPos - startPos);
force.z = force.magnitude;
force /= (Time.time - startTime);
target.AddRelativeForce(Vector3.forward *2, Force$$anonymous$$ode.Impulse);
rigidbody.AddForce(force * factor);
// ReturnBall(); }
function ReturnBall() { yield WaitForSeconds(4.0); transform.position = Vector3.zero; rigidbody.velocity = Vector3.zero; }
function OnGUI() { if(GUI.Button(Rect(0,0,100,50),"Reload")) { Application.LoadLevel(Application.loadedLevel); } }`
Your answer
![](https://koobas.hobune.stream/wayback/20220613140035im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I move an object with my finger?[C#] 1 Answer
How to trigger sound only when both fingers lifted off screen 1 Answer
A touch'es state is always Began and the position doesn't change 1 Answer
Transitioning from different number of touches causes behavior overlap 0 Answers
Please help. Attempting touch controls but I cant figure out what I'm doing wrong 3 Answers