- Home /
Dragging Camera based on Touch
In my game, I want the camera to move based on the user swiping their finger. My issue is that when someone removes their finger from the screen, and places it down again, the camera jumps to a new position... I am guessing it has to do with the coordinates, I say this because if I click somewhere far from where I removed my finger from the screen, the camera jumps. Here is the code:
var speed : int = 1;
var lastPoint : Vector2;//make this private if you want to.
function Update()
{
var offset : float; //offset of the touch from last frame
if(Input.touches.Length > 0)//make sure we have a touch in the first place
{
var evt : Touch = Input.touches[0];//setting up touch events so we can get fancy.
if(evt.phase == TouchPhase.Began)//this is the first frame the screen has been touched for, simply save the point.
{
lastPoint == evt.position;
}
else if(evt.phase == TouchPhase.Moved)
{
offset = evt.position.x - lastPoint.x ;//take the difference
//I'm going to use Transform.Rotate because it's handy dandy
transform.Rotate(0,offset * speed,0);
//save the new "lastPoint"
lastPoint = evt.position;
}
else if(evt.phase == TouchPhase.Ended)
{
//If you want the object to drift after you spin it you can make a function to go here.
//To do this, take the speed of the rotation and continue to rotate all while subtracting off of the speed.
//I would use the Transform.Rotate function on this too.
//If you need me to I could write this function too.
}
}
}
What is the solution to have the camera resume, and not jump once someone puts their finger down again?
Thanks!
Answer by Aubtin Samai · Nov 02, 2013 at 07:45 PM
I did some more research and this seems to work for me:
http://answers.unity3d.com/questions/255118/rotating-an-object-with-touch.html
Aubtin
Your answer
Follow this Question
Related Questions
Box Drag and throw away script for mobile? 0 Answers
Drag object relative to camera 1 Answer
Different animations on mobile 0 Answers
Swipe menu, problem! 0 Answers
Touch - drag different objects 1 Answer