Hello;
I m trying to convert the project from Web to Android. Im using the camera with arrow keys, i m trying to touch the screen and go left right up and down.
Here is my code in Web:
if (Input.GetKey(KeyCode.UpArrow) && Camera.main.transform.position.y < CameraYLimitUp)//Up Camera Mvt
{ transform.Translate(-Vector3.up * Time.deltaTime * 20); }
if (Input.GetKey(KeyCode.DownArrow) && Camera.main.transform.position.y > CameraYLimitDown)//Down Camera Mvt
{ transform.Translate(Vector3.up * Time.deltaTime * 20); }
if (Input.GetKey(KeyCode.LeftArrow) && Camera.main.transform.position.x > CameraXLimitLeft)//Left Camera Mvt
{ transform.Translate(Vector3.right * Time.deltaTime * 20); }
if (Input.GetKey(KeyCode.RightArrow) && Camera.main.transform.position.x < CameraXLimitRight)//Right Camera Mvt
{ transform.Translate(Vector3.left * Time.deltaTime * 20); }
Thanks for your help
You could use Unity’s joystick mobile asset and script.
Something like this?
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
int CameraYLimit;
Camera cam;
void Start ()
{
cam = Camera.main;
CameraYLimit = 10;
}
void Update ()
{
if(Input.touchCount > 0)
{
if(Input.GetTouch(0).deltaPosition.x > 0 && cam.transform.position.y < CameraYLimit)
transform.Translate(-Vector3.up * Time.deltaTime * 20);
}
}
}
i find a solution for touching the screen in the left, right, up and down
#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.