- Home /
swipe camera movement wont stay between limitations
when i touch my screen (800x480)the camera disappears. im trying to limit the area where my camera can go. i managed doing it with Clamp.
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved ) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate (touchDeltaPosition.x * Time.deltaTime * speed, touchDeltaPosition.y * Time.deltaTime * speed, 0);
transform.position = new Vector3( Mathf.Clamp(transform.position.x, minimumX, maximumX), Mathf.Clamp(transform.position.y, minimumY, maximumY), transform.position.z);
old code :
using UnityEngine;
using System.Collections;
public class MoveCam : MonoBehaviour {
public float speed = 3;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved ) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate (touchDeltaPosition.x * Time.deltaTime * speed, touchDeltaPosition.y * Time.deltaTime * speed, 0);
if (Camera.main.transform.position.x >= 1f )
{
Camera.main.transform.Translate (touchDeltaPosition.x =1f,0f,-10f);
}
if (Camera.main.transform.position.x <= -1f )
{
Camera.main.transform.Translate (touchDeltaPosition.x =-1f,0f,-10f);
}
if (Camera.main.transform.position.y >= 1f )
{
Camera.main.transform.Translate (touchDeltaPosition.y =1f,0f,-10f);;
}
if (Camera.main.transform.position.y <= -1f )
{
Camera.main.transform.Translate (touchDeltaPosition.y = -1f,0f,-10f);
}
}
}
}
Touch positions are in Screen coordinates. Your camera lives in World coordinates. You need to convert between the two. There is not enough context here for me to give you the recipe for your specific situation. Start by researching Camera.ScreenToWorldPoint(). If the camera is perspective, make sure you understand how to set the 'z' value you pass to this function.
i dont think the z coordinate actually maters. i have a pinch to zoom script. looking at the scripting API of ScreenToWorldPoint() i cant really figure out how to use it.
If the camera is perspective, then you need to specify the distance in front of the camera to do the calculation. If you enter 0.0 for 'z', Camera.ScreenToWorldPoint() will return the position of the camera for all values. Note you don't have to position the camera at the 'z' value returned, but you need some distance in 'z' for a perspective.
Answer by Nerull22 · Jul 28, 2014 at 03:05 PM
So I think where your problem is that you're directly relating the touch input into the transform of your camera, if I'm reading this correctly. When you touch, it's in screen positioning instead of world units.
So when you first press down and the touch phase is "Began" then record the position. Then convert that from screen units to world units. Camera.ScreenToWorldUnits I believe is the method, but check the unity script to make sure.
So when you move, convert the position to world units and then you can measure the world units instead of screen. Because right now, I believe when you swipe your sending your camera hundreds of world units out of position, which is why you're getting the effect.
Let us know if you're still having problems.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
iTween Path changing 0 Answers