- Home /
Camera Touch pan movement inconsistent across different screen resolutions.
I currently have this code.
if (Input.touchCount == 1) {
if (Input.GetTouch (0).phase.Equals (TouchPhase.Moved)) {
NewPos += new Vector3 (
-Input.GetTouch(0).deltaPosition.x/Screen.width*panSpeed,
0,
-Input.GetTouch(0).deltaPosition.y/Screen.height*panSpeed
);
}
}
which is my attempt to normalize the Touch deltaPositions. but even after dividing with screen width/height, I still notice a massive difference in speed when running it on my phone, compared to the smaller window in unity.
My phone has a resolution of 1280x720. and android phones range from everything between 480p to 2k screen resolutions.
Is there a way to make the camera movement speed consistent across all devices?
Is this in, ummm, Update? I assume, like other Input events, FixedUpdate can miss or double-count, depending on frame rate. Ohterwise it looks fine to me.
Yeah it's in update, and i set the actual position of the camera in fixed Here's the whole code http://pastebin.com/CzNzu3q6
Answer by dheffner2001 · Oct 20, 2020 at 08:09 PM
This post is from 2014 but i think many people might have the same question. This is my solution:
public float minXPos = -12f;
public float maxXPos = 100f;
public float minYPos = -100f;
public float maxYPos = 100f;
void Update()
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
float screenSize = Mathf.Max(Screen.width, Screen.height);
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
Vector2 touchDeltaPercentage = touchDeltaPosition / screenSize;
Vector3 screenCenter = new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 1f);
Vector3 screenTouch = screenCenter + new Vector3(touchDeltaPosition.x, touchDeltaPosition.y, 0f);
Vector3 worldCenterPosition = Camera.main.ScreenToWorldPoint(screenCenter);
Vector3 worldTouchPosition = Camera.main.ScreenToWorldPoint(screenTouch);
Vector3 worldDeltaPosition = worldTouchPosition - worldCenterPosition;
//touchDeltaPercentage values will now range from 0 to 1
//define camera border in minXPos, maxXPos, minYPos and maxYPos
transform.Translate(-worldDeltaPosition);
Vector3 tmpPosX = transform.position;
tmpPosX.x = Mathf.Clamp(tmpPosX.x, minXPos, maxXPos);
transform.position = tmpPosX;
Vector3 tmpPosY = transform.position;
tmpPosY.y = Mathf.Clamp(tmpPosY.y, minYPos, maxYPos);
transform.position = tmpPosY;
}
}
This worked very nicely for me, thank you! If anybody is looking for a version for mouse this is my version:
if (Input.GetMouseButtonDown(2))
{
deltaMousePos = Input.mousePosition;
}
else if (Input.GetMouseButton(2))
{
Vector2 mouseDeltaPosition = Input.mousePosition - deltaMousePos;
Vector3 screenCenter = new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 1f);
Vector3 screenTouch = screenCenter + new Vector3(mouseDeltaPosition.x, mouseDeltaPosition.y, 0f);
Vector3 worldCenterPosition = Camera.main.ScreenToWorldPoint(screenCenter);
Vector3 worldTouchPosition = Camera.main.ScreenToWorldPoint(screenTouch);
Vector3 worldDeltaPosition = worldTouchPosition - worldCenterPosition;
//touchDeltaPercentage values will now range from 0 to 1
//define camera border in minXPos, maxXPos, minYPos and maxYPos
transform.Translate(-worldDeltaPosition * panMultiplier);
Vector3 tmpPosX = transform.position;
tmpPosX.x = Mathf.Clamp(tmpPosX.x, minXPos, maxXPos);
transform.position = tmpPosX;
Vector3 tmpPosY = transform.position;
tmpPosY.y = Mathf.Clamp(tmpPosY.y, minYPos, maxYPos);
transform.position = tmpPosY;
deltaMousePos = Input.mousePosition;
}
Your answer
Follow this Question
Related Questions
How to avoid TouchPhase.Ended effect? 0 Answers
Adapting camera movement to touch input 0 Answers
Change a float value with the horizontal axis? 1 Answer
Multitouch problem 0 Answers