Perspective camera touch movement
Hi
I have a problem configuring movement of camera with touch input. I found a script on the forums that should do the job but the problem is that camera wont move over z axis. It is perspective camera so I want it just to be moved over x and z axis for purpose of kinda rts like game.
So when I move finger on the touch screen in x axis direction it works but y axis is constantly 0. I cant see where this problem occurs and why. I also need to put that y axis movement on touch screen to z axis transform of camera.
This is the code I found:
void Update ()
{
if (Input.touchCount == 1) {
Touch currentTouch = Input.GetTouch(0);
if (currentTouch.phase == TouchPhase.Began) {
this.worldStartPoint = this.getWorldPoint(currentTouch.position);
}
if (currentTouch.phase == TouchPhase.Moved) {
Vector2 worldDelta = this.getWorldPoint(currentTouch.position) - this.worldStartPoint;
Camera.main.transform.Translate( -worldDelta.x, 0, -worldDelta.y );
}
}
}
private Vector2 getWorldPoint(Vector2 screenPoint)
{
RaycastHit hit;
Physics.Raycast(Camera.main.ScreenPointToRay(screenPoint), out hit);
return hit.point;
}
Answer by rafaelbarretobra · Feb 07, 2017 at 03:18 PM
Where is your worldStartPoint variable? How did you define it? Also, I think the translate should be on x and z, not x and y.
Answer by invo1 · Aug 01, 2017 at 08:19 AM
you should specify that it moves on World space and not taking your local coordinates
Camera.main.transform.Translate(-worldDelta.x, 0, -worldDelta.y,Space.World);
Your answer
Follow this Question
Related Questions
Android Touch - how to know on which camera i touch 0 Answers
Why does my transform.lookat not work? 1 Answer
Transform.Rotate x and y rotate z, where z is 0 0 Answers
Limit game object to camera view 0 Answers