- Home /
Camera Movement and sprites Touch Unity2d android
Hi, I m developing an android 2d game , i m moving a camera with touch left , right , up and down when i touch anywhere to the device screen it's moving. and i have a sprite , when i click on it shows me a message that i m clicking on sprite. Here is the code that is attached to the camera:
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
startPos = touch.position;
fingerHold = true;
}
else if (touch.phase == TouchPhase.Moved)
{
endPos = touch.position;
}
else if (touch.phase == TouchPhase.Ended)
{
fingerHold = false;
}
}
if (fingerHold)
{
float deltaX = endPos.x - startPos.x;
float deltaY = endPos.y - startPos.y;
bool horizontal = false;
if (Mathf.Abs(deltaX) > Mathf.Abs(deltaY))
horizontal = true;
if (horizontal)
{
if (deltaX < 0 && transform.position.x < CameraXLimitLeft)
{
transform.Translate(Vector3.left * Time.deltaTime * 20);
}
else if (deltaX > 0 && transform.position.x > CameraXLimitRight)
{
transform.Translate(Vector3.right * Time.deltaTime * 20);
}
}
else
{
if (deltaY < 0 && transform.position.y < CameraYLimitDown)
{
transform.Translate(Vector3.down * Time.deltaTime * 20);
}
else if (deltaY > 0 && transform.position.y > CameraYLimitUp)
{
transform.Translate(Vector3.up * Time.deltaTime * 20);
}
}
}
And this is the script that attached to the sprite.
public GUIText guiTextSprite;
void OnMouseDown()
{
Debug.Log("Test Sprite");
guiTextSprite.text = "Test Sprite";
}
PS: in the sprite there is a polygon collider2d , sprite renderer and rigidbody.
The problem that when i touch the sprite the camera still moves. I want when i touch the sprite the camera stop moving and when i touch anywhere to the screen the camera moves
Thanks for your help
Answer by IronGirl · Sep 02, 2014 at 02:57 PM
i find a solution for my problem here is the script attached to the camera to let the camera moves.
#region Touch Mobile
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * speed * Time.deltaTime, -touchDeltaPosition.y * speed * Time.deltaTime, 0);
Vector3 tmpPosX = transform.position;
tmpPosX.x = Mathf.Clamp(tmpPosX.x, -13.0f, 14.0f);
transform.position = tmpPosX;
Vector3 tmpPosY = transform.position;
tmpPosY.y = Mathf.Clamp(tmpPosY.y, -7.0f, 18.0f);
transform.position = tmpPosY;
}
#endregion
this script let me move the camera when i touch the screen in the left , right , up and down and let me limit the movement of the camera in X and Y Axis.
for the Sprite here is the script.
public float tapSpeed = 0.5f;
private float lastTapTime = 0;
void OnMouseDown()
{
if((Time.time - lastTapTime) < tapSpeed)
{
Debug.Log("Double tap");
}
lastTapTime = Time.time;
}
This script is attached to the spriteand let you Double tap the sprite and when i double tap on sprite , the camera doesn't move.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160333im_/https://answers.unity.com/themes/thub/images/avi.jpg)