- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
shygeek · Jun 25, 2014 at 12:28 PM ·
gameobject3dswipe
Football penalty game- Move gameobject in direction of swipe with force
Hello !! I want to make a game in which the user swipes the ball (like kicking the football) and the goalkeeper tries to save it. I also want to swipe the ball in the direction of swipe and also force. In my below code it only checks upward swipe. Can anyone help me how to proceed ?
var gravity : float = 20.0;
var dis:float=20.0;
var uptest:boolean=true;
var downtest:boolean=true;
var StartPos:Vector2;
var finid:int=-1;
private var moveDirection : Vector3 = Vector3.zero;
function Start()
{
Physics.gravity = Vector3(0,-25,0);
}
function Update () {
for(var touch:Touch in Input.touches)
{
var pos = touch.position;
if (touch.phase == TouchPhase.Began && finid == -1)
{
finid = touch.fingerId;
StartPos = pos;
}
else if (touch.fingerId == finid)
{
var delta = pos - StartPos;
if (touch.phase == TouchPhase.Moved && delta.magnitude > dis)
{
finid = -1;
if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
{
//
}
else
{
if (uptest && delta.y > 0)
{
//upswipe
}
else if (downtest && delta.y < 0)
{
//downswipe
}
}
}
else if (touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended)
finid = -1;
}
}
}
Comment
I have a code which just detects upward swipe. I want to modify this to move the gameobject in the direction of swipe with the force.
Detect the first touch position.y and compare it with the touch endpoint.position.y Now you now how much force you can give the ball to either left or right.
var anglex : float;
var angley : float;
var elevationAngle : Vector3;
function Awake () {
if (ScreenOrientation.Landscape)
{
Screen.orientation = ScreenOrientation.LandscapeLeft;
}
else
{
Screen.orientation = ScreenOrientation.Portrait;
}
}
function FixedUpdate ()
{
if(Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
var touchdeltaposition: Vector2 = Input.GetTouch(0).deltaPosition;
anglex = touchdeltaposition.x * Time.deltaTime *2.0;
Debug.Log(anglex);
angley = touchdeltaposition.y * Time.deltaTime *5.0;
Debug.Log(angley);
elevationAngle = Vector3(anglex,angley,0);
rigidbody.velocity = transform.TransformDirection(Vector3(anglex,angley,angley ));
var elevation : Vector3 = Quaternion.Euler(elevationAngle) * transform.forward;
rigidbody.AddRelativeForce(elevation);
Debug.Log("touch end");
}
}