I've been trying to get my head around a solution for my camera movement. These are the parameters it needs to work by and some thoughts I had tried:
So my thoughts have lead me to devise a solution to grab the Vector3 point where the ray hits the ground under the finger on fingerDown then move the camera in an opposite direction to the real world distance between the origin and movingPoint. If that makes any sense.
I'm pretty confused about it so could someone either confirm or deny my idea and give a clear way forward =)
I'm using an asset called LeanTouch (brilliant piece of free scripts) for the touch inputs and this is as far as my brain wants to go: (lol)
var finger = Lean.LeanTouch.Fingers [Lean.LeanTouch.Fingers.Count - 1];
var ray = finger.GetRay ();
int layerMask = (1 << 8);
Physics.Raycast (ray, out hit, Mathf.Infinity, layerMask);
InitialHit = hit.transform.position;
Btw i'm working in Unity and C#
Ok, I finally got it. A nice smooth accurate camera movement, based on the physical locations on the map from where my fingers touch the screen.
using UnityEngine;
public class SimpleCamMove : MonoBehaviour
{
private RaycastHit hit;
private Vector3 InitialHit;
private Vector3 CurrentHit;
private Vector3 DirectionHit;
private bool CamActive;
protected virtual void OnEnable()
{
// Hook into the OnFingerDown event
Lean.LeanTouch.OnFingerDown += OnFingerDown;
// Hook into the OnFingerUp event
Lean.LeanTouch.OnFingerUp += OnFingerUp;
}
protected virtual void OnDisable()
{
// Unhook the OnFingerDown event
Lean.LeanTouch.OnFingerDown -= OnFingerDown;
// Unhook the OnFingerUp event
Lean.LeanTouch.OnFingerUp -= OnFingerUp;
}
public void OnFingerDown(Lean.LeanFinger finger)
{
//Fires ray with ScreenToWorld at finger pos using LeanTouch
var ray = finger.GetRay ();
int layerMask = (1 << 8); //ground layer
CamActive = true;
if (Physics.Raycast (ray, out hit, 1500, layerMask)) {
InitialHit = hit.point;
}
}
protected virtual void LateUpdate()
{
if (CamActive == true) {
var finger = Lean.LeanTouch.Fingers [Lean.LeanTouch.Fingers.Count - 1];
var ray = finger.GetRay ();
int layerMask = (1 << 8);
if (Physics.Raycast (ray, out hit, 1500, layerMask)) {
CurrentHit = hit.point;
DirectionHit = (CurrentHit - InitialHit);
//Camera stays at same height.
//Invert coords to move camera the correct direction
this.transform.Translate(
new Vector3(-DirectionHit.x,0,-DirectionHit.z));
}
}
}
public void OnFingerUp(Lean.LeanFinger finger)
{
CamActive = false;
}
}