- Home /
RTS touch camera movement
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:
Camera movement / not world movement
Raycast to move camera accurately along terrain
Touch movement works by "grabbing" the terrain towards you or "pushing" it away
Screen space to world space will not work because of the angle of the camera
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#
Answer by 4t0m1c · May 20, 2015 at 12:08 PM
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)
{
var ray = finger.GetRay (); //fires ray with ScreenToWorld at finger pos using LeanTouch
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);
this.transform.Translate(new Vector3(-DirectionHit.x,0,-DirectionHit.z)); //camera stays at same height. Invert coords to move camera the correct direction
}
}
}
public void OnFingerUp(Lean.LeanFinger finger)
{
CamActive = false;
}
}
Your answer
Follow this Question
Related Questions
Camera Follow works partially, need help to finish the script 3 Answers
How do I program camera movement 2 Answers
My camera won't move properly. 1 Answer