- Home /
Pan Orthographic camera in 3D
How do I pan a camera with touch(mouse drag also works), which is rotated on the X by 30 and on the Y by 45 degrees. I want the camera to move like in Clash of Clans or similar strategy games.
Here's what I have right now, but it doesn't work properly:
public class CameraMovement : MonoBehaviour {
public Transform camDrone;
public Camera mainCam;
public float camSpeed;
// Update is called once per frame
void Update () {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchPos = Input.GetTouch (0).deltaPosition;
Vector3 camPos = new Vector3 (-touchPos.x, 10f, -touchPos.y);
camDrone.Translate (camPos * camSpeed * Time.deltaTime);
}
}
}
Answer by springwater · Jul 13, 2017 at 02:53 PM
I think, rather than translate, you will want to use lerp or Vector3. Lerp or whatever it's called and create way points as necessary. Just check out lerp the documentation.
The problem is, that my camera is at an angle. Doesn't matter if I use Lerp or Translate. I just need to know how I make camera movement with touch in a 3D game.
Answer by idontneedthat · Jul 13, 2017 at 03:17 PM
I think you should rewrte it and as your camera is rotated it will not work as you expect. I think you should rewrte it and as your camera is rotated it will not work as you expect.
Answer by TWicked · Jul 14, 2017 at 08:17 AM
Simple solution would be to an empty game object and add camera to it as a child. When you rotate/move that game object, camera will move with it keeping intended orientation.
Answer by AtGfx · Jul 13, 2017 at 03:08 PM
I think that your code nearly works. Just specify Space.World as the second argument of Translate method. If you left it unset, the local basis of object is used, and as your camera is rotated it will not work as you expect.
This works, but whenever I touch again, it goes back to the camera initial position.
void Update () {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
Touch touch = Input.GetTouch (0);
// create a ray from the current touch point into the world
Ray curPosRay = Camera.main.ScreenPointToRay( touch.position );
// fire that ray out and see if it hits anything
RaycastHit curHitInfo;
if( Physics.Raycast( curPosRay, out curHitInfo ) )
{
// we have a hit, do the same for the last position
Ray lastPosRay = Camera.main.ScreenPointToRay( lastPosition );
RaycastHit lastHitInfo;
if( Physics.Raycast( lastPosRay, out lastHitInfo ) )
{
// get the delta from those positions
Vector3 deltaPos = curHitInfo.point - lastHitInfo.point;
// zap y changes, we're only concerned about the x/z plane
deltaPos.y = 0;
// reverse the direction to negate the movement such that the touch point
// stays over the same spot once the camera moves.
deltaPos *= -1;
// apply the pan
camDrone.Translate(deltaPos * Time.deltaTime * camSpeed);
}
}
lastPosition = touch.position;
}
}
$$anonymous$$aybe you should record a vule in lastPosition when user touch the screen, then execute your function when user move his finger. Otherwise when you take your finger off the screen you have the lastPosition recorded and when you move again in another spot, the first execution of your method will use the last position recorded for the last move on the touch screen.
Your answer
Follow this Question
Related Questions
pan camera around with buttons or touch 0 Answers
Rotate, pan and zoom camera lerp touch 0 Answers
pan camera by touch / gui button 0 Answers
Force 3D touch IOS 1 Answer
How to use two cameras in VR to display 3D object on top of UI panel? 0 Answers