- Home /
moving an object with swipe for ios?
Actually, in this code i am taking one ball as a object, i want to swipe that ball and move towards the goal post. and i want to set the angle for that ball according to X axis displacment and for projectile according to y axis displacment . and also i want to give the speed according to swipe of fingure on ball. please help me.
thank you for your help.
var Ball : GameObject; // football is taking as an object
function Update ()
{
if(Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
var touchdeltaposition: Vector2 = Input.GetTouch(0).deltaPosition;
anglex = touchdeltaposition.x ;
angley = touchdeltaposition.y ;
elevationAngle = Vector3(anglex,0,0); // angel for ball movment
var throwForce:int = 1000;
Ball.rigidbody.velocity = transform.TransformDirection(Vector3(anglex,angley, 0)); // projectile of ball on the plan
var elevation : Vector3 = Quaternion.Euler(elevationAngle) * transform.forward;
ball.rigidbody.AddForce(elevation * throwForce);
Debug.Log("touch end");
}
}
Answer by Graham-Dunnett · Nov 21, 2012 at 11:00 AM
I've no idea what your code is supposed to do. Perhaps edit your question and describe step by step what that code is meant to do. Here's how I would tackle the same problem (not that I have ever attempted to write this code..)
I'm guessing that your screen has the goal visible, and there is a ball somewhere on the screen. The top and bottom of the two goal posts define 4 points in 3d space, and 4 points in 2d screen space. The ball defines a 5th position in 3d space, and 5th position in 2d screen space. If I did a short swipe from the ball towards say the top right hand goal post, I'd want the ball to be kicked in that direction, just not very fast. If instead I did a longer or faster swipe to the same point, then that would be a stronger kick. So, I'd look to see where the swipe started and ended, and how long that took. This gives me some idea about the speed of the kick. Next, I'd work out from the 4 2d screen coordinates representing the top and bottom of the goal posts, and the end point of the swipe where in the 3d space of the goal the goal the ball needs to be kicked. This then gives you a direction to kick the ball.
thanks for your replay sir, i edited my question. the main problem is that i want to move the ball on 3d space according to our touch. like that " throw a ball towards Z direction with speed and angular projectile of that ball on plane". i hope that now you are understanding my problem.
and again thanks for your valuable reply.
Answer by deepak-impressol · Nov 24, 2012 at 09:12 AM
finally i solved my problem. and i got perfect direction of ball . i am putting my code , may be some people can use it . thanks. 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");
}
}
Thanks for the script. I have tried using this script on a car though when I swipe nothing happens. I am trying it on unity remote android. If I assign it to the certain game object does it also work on other object (not desired). Is there something special that needs to be done because my car does not move when swiped in remote unity. Thanks (realise it is an old topic!)
Your answer
Follow this Question
Related Questions
Please tell me what is wrong with this Projectile.js file 1 Answer
How to get my character to move left to right? 0 Answers
Touch teleport script isnt working 2 Answers
A node in a childnode? 1 Answer