- Home /
Touch and drag in the middle of the screen to rotate camera around the player
I'm currently rotating the camera around the player using only this piece of code.
void Update(){
if (Input.GetMouseButton(0)){
transform.LookAt(target);
transform.RotateAround(target.position, Vector3.up, Input.GetAxis("Mouse X") * speed);
offset = transform.position - target.position;
}
}
By clicking and holding on the left mouse button, i can rotate the camera around the player. How can i do the same but using Touch instead of Mouse, and only do this when clicking on the center of the screen. Thanks in advance.
Answer by Serge144 · Feb 27, 2020 at 07:20 PM
So apparently the getMouseButton(0) can work as touch input on mobile, so i reused what i already had and did like this: please tell me if there's a better way to do it.
void Update(){
if (Input.GetMouseButton(0)){
int middleX = Screen.width / 2;
int middleY = Screen.height / 2;
Vector3 leftBottom = new Vector3(middleX - windowSize, middleY - windowSize);
Vector3 rightBottom = new Vector3(middleX + windowSize, middleY - windowSize);
Vector3 leftTop = new Vector3(middleX - windowSize, middleY + windowSize);
Vector3 rightTop = new Vector3(middleX + windowSize, middleY + windowSize);
if (Input.mousePosition.x > leftBottom.x && Input.mousePosition.x < rightBottom.x) {
if (Input.mousePosition.y > leftBottom.y && Input.mousePosition.y < rightTop.y) {
transform.LookAt(target);
transform.RotateAround(target.position, Vector3.up, Input.GetAxis("Mouse X") * speed);
offset = transform.position - target.position;
}
}
}
}
actually, this doesn't work very good, because if i drag from inside to outside of the box it stops rotating..
Your answer
Follow this Question
Related Questions
Unity mobile swipe drag control 0 Answers
Camera rotation as per swipe speed & it has to slow down as gradually? 0 Answers
unity2D touch drag 0 Answers
Different resolutions make the swipe of my player go at different speeds on different devices 0 Answers
Character keeps snapping between two finger positions [Mobile, Android, Raycast] 0 Answers