- Home /
Question by
CatCatC · Jun 10, 2018 at 04:20 AM ·
2dcamera movementdrag and drop
Camera dragging problem in 2D
I'm trying to drag the main camera in a 2D game.
But there are some problems.
1. The camera jerks around a lot.
2. The camera moves a lot slower than the mouse.
By the way, my camera is in Perspective mode.
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera : MonoBehaviour {
bool las;
Vector3 pos = new Vector3(0,0,-10),laspos;
Vector3 GetMousePos(){
Vector3 mpos = Input.mousePosition;
mpos.z = -pos.z;
return Camera.main.ScreenToWorldPoint(mpos);
}
void Update(){
if(Input.GetMouseButton(0)){
if(las){
Vector3 dxy = GetMousePos();
pos.x -= dxy.x - laspos.x;
pos.y -= dxy.y - laspos.y;
}
las = true;
laspos = GetMousePos();
}
else las = false;
float ddis = Input.GetAxis("Mouse ScrollWheel");
pos.z *= 1 - ddis;
if(pos.z < -70f) pos.z = -70f;
if(pos.z > -3f) pos.z = -3f;
}
void LateUpdate(){
transform.position = pos;
}
}
What's wrong with it?
Comment
Your answer
Follow this Question
Related Questions
2d beat em up clamp player when leaving another player 1 Answer
Why is 2d camera not smooth at 30 FPS 0 Answers
2d camera movement with Lerp problem 0 Answers
2D game camera zoom in/out (JS) 1 Answer