- Home /
Camera touch control script
I need camera controll by touchscreen like in angry bird and many other 2d games(it look like scrolling).
It's like scroll bar - Did you play angry birds? Whan you want to see al level you scroll to left and camera moves to right. I hope somebody understand my poor English
A polite question will give you more answers, I can assure you. Your knowledge of english surely includes "Hello" and "please".
It's pretty easy - if you can't write this, you'll have trouble with the rest of your game.
Just read in each of the 'Touch' objects in Input.touches and you will be able to then move your camera to suit.
Each touch has a 'phase' - Begun, $$anonymous$$oved, Ended or Cancelled etc.
When you see a touch begin, record where it happened. Then when you see '$$anonymous$$oved' you adjust the camera position based on the difference between the CURRENT position and the position of the initial touch.
I understand, but if we are going to discuss tone we should probably also view the tone used in the original question.
The poster didn't ask for help - they asked for a complete script to do to he job. It may be why they have no answers.
As it is, I pointed the user towards the technique. The moment they come back with more questions (ideally after reading some of the documentation on dealing with touch events and starting a behaviour in their language of choice) I'm more than happy to help with actual code.
The purpose of this forum is to HELP, not to provide free code on demand.
Answer by IgnoranceIsBliss · Apr 15, 2012 at 11:46 PM
A rough guide to moving the camera based on touch.
This is in C# and moves the camera on the Y axis only. It's pretty rough and hasn't been tested.
If you wanted to fiddle, you could add momentum (so the camera continues to move after your finger raises) and a scaling factor to the deltaPosition so you can turn sensitivity up or down.
class TouchCameraMover
{
// The ID of the touch that began the scroll.
int ScrollTouchID = -1;
// The position of that initial touch
Vector2 ScrollTouchOrigin;
void Update()
{
foreach(Touch T in Input.touches)
{
//Note down the touch ID and position when the touch begins...
if (T.phase == TouchPhase.Began)
{
if (ScrollTouch == -1)
{
ScrollTouchID = T.fingerId;
ScrollTouchOrigin = T.position;
}
}
//Forget it when the touch ends
if ((T.phase == TouchPhase.Ended) || (T.phase == TouchPhase.Canceled))
{
ScrollTouchID = -1;
}
if (T.phase == TouchPhase.Moved)
{
//If the finger has moved and it's the finger that started the touch, move the camera along the Y axis.
if (T.fingerId == ScrollTouchID)
{
Vector3 CameraPos = Camera.main.transform;
Camera.main.transform = new Vector3(CameraPos.x,CameraPos.y + T.deltaPosition.y,CameraPos.z);
}
}
}
}
}
1st of all thanks for such a nice script it works perfectly for my case but I want to add momentum in movement how can i do that .
All you'd need to do is store the speed that the user was scrolling when they released the button (ie the number of pixels per second, using Time.deltaTime).
So for example, you calculate speed with...
Speed = T.deltaPosition / Time.deltaTime;
Then after they stop touching the screen, keep moving at that speed. Every frame, drop the speed by a certain amount. If you were dropping the speed by 5 pixels per second, you could do so with...
Speed = Speed - (5 * Time.deltaTime);
if we have more people like you, this world would be a better place. thanks alot for your help
Your answer
Follow this Question
Related Questions
Touch not working if camera is moved 0 Answers
perspective camera into android 0 Answers
Rotate Camera around Object 2 Answers
i want to help touch functionalities for android how will i do that ?? 0 Answers
Rotate Camera around object with touch 0 Answers