- Home /
Help with touch camera movement
Well, im new to Unity and touch based events in general. Im trying to make the camera move on the X/Y coordinate plane with touch. I have been able to get it to move, and its moving in the right direction (so I think) but its moving in extreme steps. Im using the cameras beginning position and take the new delta position from it, and updating the camera postion that way. From what I can tell it is moving in the right direction, but its jumping by the thousands per short movement. Ive even tried to multiply the deltaposition by .001 and still no luck. Im pretty sure this is an easy fix. Here is the code
void Update () {
foreach(Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
if (touch.fingerId == 0) {
//Get starting position of touch
touchBound = touch.fingerId;
ScrollTouchOrigin = touch.position;
}
}
//Unbind the touch if the touch phase ends
if (touch.phase == TouchPhase.Ended) {
touchBound = -1;
ScrollTouchOrigin = Vector2.zero;
}
//Do movement on touchphase move
if (touch.phase == TouchPhase.Moved) {
//Only move if the touch that is bounds is the one that moved
if (touch.fingerId == touchBound) {
//Get current camera postion
Vector3 oldPos = Camera.main.transform.position;
Camera.main.transform.Translate(new Vector3(oldPos.x - (touch.deltaPosition.x * 0.001f), oldPos.y - (touch.deltaPosition.y * 0.001f), 5.0f));
mGuiTextX.text = Camera.main.transform.position.x.ToString();
mGuiTextY.text = Camera.main.transform.position.y.ToString();
}
}
}
}
Answer by robertbu · May 26, 2013 at 07:48 PM
Note that touch.deltaPositon is in Screen coordinates and your camera lives in world coordinates. You can just define a factor between the two, or you could derive a factor based on screen resolution so that the movement is consistent across devices. But in terms of your code you can change it in one of two ways. Since Translate is a relative movement, you can do:
Camera.main.transform.Translate(touch.deltaPosition * 0.001);
Where .001 is a factor that you either hard code or derive from the screen resolution.
Or you can do an absolute movement which is what you code was trying to do:
Camera.main.transform.position(new Vector3(transform.position.x - (touch.deltaPosition.x * 0.001f), transform.position.y - (touch.deltaPosition.y * 0.001f), 5.0f));
Note this assumes that you want the camera's 'z' positon to be 5.0.
Thanks, the first option worked perfect. The only thing I had to do differently was multiply the delta position by a negative 0.1f and it worked.
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Clamp X position of object 1 Answer
Middle of screen vector 3? 2 Answers
Question on Vector3.Reflect() 2 Answers
How to cut numbers after decimal point in coordinates? 1 Answer