- Home /
Can you use UI Scroll Rect for camera movement?
I have a 2D scene that uses pinch zoom. Of course the zoom only goes into the center of the scene, so I would like to add a function that allows the user to drag the scene, allowing them to see other areas. I've had similar effects using the UI Scroll Rect feature, but I had a UI panel with all the elements on the panel. It doesn't really seem practical to make everything in the scene a child of a panel and there doesn't seem to be any way to add a rect transform to a camera (which makes sense). Is there another easy way to do this?
Note: I realize there's probably a way to do this in script, but before researching, writing and testing a script, I want to make sure there isn't a super easy & fast way to do this in the Unity editor, which has happened to me numerous times in he past.
Answer by fafase · May 14, 2015 at 07:58 PM
If you have the pinch working you can also use it to move around. Take the middle point of your two touches and compare it over frame. The delta you get can be applied to Translate method on your camera.
void Update(){
Vector3 pinch = (touch1.position - touch0.position);
Vector3 newPos = touch0.position + pinch * 0.5f;
Vector3 translate = newPos - oldPos;
Camera.main.transform.Translate(translate * Time.deltaTime);
oldPos = newPos;
}
This is the basic idea as you need to consider how your camera can move.
Your answer
Follow this Question
Related Questions
Is it good idea to implement camera drag with UI drag event? 2 Answers
Drag on Scroll Rect Non Proportional 0 Answers
ScrollRect scrollbar drag being overridden and stuttering to top 0 Answers
Different Camera changes work against each other? 1 Answer
Prevent Drag button inside scroll rect 0 Answers