I made a script to move the camera on a terrain containing montains on mobile device.
I am trying to make a movement script for Android to move the camera over a terrain like it is done on Google maps.
public LayerMask terrainMask;
// This mask need to be not used elsewhere
// Needed for raycasting
public int layerAlone;
GameObject[] previousTouchPointAndInputMask;
void Start()
{
previousTouchPointAndInputMask = new GameObject[3];
for (int i = 0; i < 3; i++)
{
previousTouchPointAndInputMask[i] = new GameObject("PlatformController Input platform");
previousTouchPointAndInputMask[i].AddComponent<BoxCollider>().size = new Vector3(float.MaxValue, 1, float.MaxValue);
previousTouchPointAndInputMask[i].layer = layerAlone;
}
}
void LateUpdate()
{
Ray ray;
UnityEngine.RaycastHit hitInfo;
if (Input.GetButtonDown("Fire1") && Input.touchCount == 1)
{
// Construct a ray from the current mouse coordinates
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hitInfo, float.MaxValue, terrainMask);
previousTouchPointAndInputMask[0].transform.position = hitInfo.point;
previousTouchPointAndInputMask[0].transform.localRotation = Quaternion.identity;
}
else if (Input.GetButton("Fire1") && Input.touchCount == 1)
{
// Construct a ray from the current mouse coordinates
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitInfo, float.MaxValue, 1 << layerAlone))
{
Vector3 movement = previousTouchPointAndInputMask[0].transform.position - hitInfo.point;
movement.y = 0;
transform.Translate(movement);
}
}
}
This script is attach to the camera.
So i am working with ray cast.
To avoid montains to block raycasting, after the initial raycast on terrain, next raycast are done on an invisible horizontal plane.
This script is working to move horizontally on any terrain.
Now I am trying to make similar things with two fingers (to apply rotation and zoom in zoom out) but I can't find a solution.
I tried to apply one ray cast per fingers, then i don't know what to do with the data.
Any idea ?