I want to rotate the 2D missile with touch controls
Hi i'm making a game and i spent days to resolve my problem but i didn't succeeded my problem is i want to control a missile with touch i will explain , first i will touch anwhere on the screen and if i rotate my finger to the left the gameobject must rotate left and vice versa , the game object have a rigidbody2D can anyone help me please
Answer by royhammel · Dec 22, 2017 at 02:20 AM
I can't imagine how you'd do this with either capacitive or resistive touch screens. you would need a second finger to track or you would need to track the angle the finger was dragged out instead of keeping it in one place. this is because current touch screen technology only tracks the touch location and not the orientation of the finger. another possibility is the use of custom hardware to track the rotation, but that would be a pretty hard road to go down.
to do the two-finger solution, track either the angle or angle change (depending on how you want it to act) between the fingers and use that for rotation. for the dragging solution, track the first touch location and use the current location as well as the location where you let your finger up as the second location for tracking the angle. arctangent is your goto trig function here.
hey royhammel , Thank you for your response i really appreciate your help , i think i didn't explain my problem very well all i want is when i drag my finger on the screen on a rotation mouvement (not rotate my finger) the gameobject will rotate also (like the game GoPlane on playstore) could you help mye with this please ?
yes. I can help you with it. it's actually pretty simple. what you want is the change in the angle from one frame to the next of the rotation of your finger's position around the game object. this change in angle is what you apply to the game object.
you get this by feeding the vector resulting from the position of the game object subtracted from the position of your finger on the screen. there is a very convenient function for doing this called atan2(). it's a special implementation of arctangent that takes a delta x and a delta y and returns an angle. always keep track of the previous angle while the user's finger is still on the screen. the difference between that angle and the current angle is the actual change you apply to the game object's rotation.