Hey so I’m working on a simple phone game but are having some really annoying problems with the camera moving fast between 2 points but I’m only changing the position once in my script. I’ve uploaded the project here for demonstration: https://dl.dropboxusercontent.com/u/13581369/SomethingIsNotRight/SomethingIsNotRight.html (Hold down button 0/LMB and move slowly)
My goal is to have smooth movement relative to the world when draging the mouse or finger on the phone. my very messy script so far:
using UnityEngine;
using System.Collections;
public class Swipe : MonoBehaviour
{
// Use this for initialization
void Start ()
{
Application.targetFrameRate = 60;
}
float speed = 0.1f;
float zoomSpeed = 0.1f;
float currDist = 0.0f;
float lastDist = 0.0f;
Vector3 LastPosition;
Vector3 CurrentPosition;
void FixedUpdate()
{
CurrentPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
private void LateUpdate()
{
//Mobile
//if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
//{
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
StopAllCoroutines();
flicktimer = 0;
CheckClick(Input.GetTouch(0).position);
}
else if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
this.transform.position += (Vector3)Input.touches[0].deltaPosition;
}
else if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
CheckClick(Input.GetTouch(0).position);
if (CheckFlick())
{
flickstart = this.transform.position;
flickend = this.transform.position;
flickend -= (Input.touches[0].deltaPosition * 0.0001f);
StartCoroutine(DoFlick());
}
}
}
if (Input.touchCount == 2)
{
PinchZoom();
}
//}
//PC
//else if (Application.platform == RuntimePlatform.WindowsEditor)
//{
if (Input.GetMouseButtonDown(0))
{
StopAllCoroutines();
CheckClick(Input.mousePosition);
}
else if (Input.GetMouseButton(0))
{
this.transform.position += (LastPosition - CurrentPosition);
}
else if (Input.GetMouseButtonUp(0))
{
CheckClick(Input.mousePosition);
if (CheckFlick(CurrentPosition, LastPosition))
{
flickstart = this.transform.position;
flickend = this.transform.position;
flickend -= (Vector2)((CurrentPosition - LastPosition) * 0.01f);
StartCoroutine(DoFlick());
}
}
Zoom();
//}
LastPosition = CurrentPosition;
}
private bool CheckClick(Vector2 pos)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
Vector2 touchPos = new Vector2(wp.x, wp.y);
var hit = Physics2D.OverlapPoint(touchPos);
if (hit)
{
Debug.Log(hit.transform.gameObject.name);
//hit.transform.gameObject.SendMessage('Clicked',0,SendMessageOptions.DontRequireReceiver);
return true;
}
return false;
}
private bool CheckFlick()
{
Debug.Log((Input.touches[0].deltaPosition));
return (Vector2.SqrMagnitude(Input.touches[0].deltaPosition) > 10);
}
private bool CheckFlick(Vector2 curpos, Vector2 lastpos)
{
Debug.Log(Vector2.SqrMagnitude(curpos - lastpos));
return (Vector2.SqrMagnitude(curpos - lastpos) > 10);
}
private void PinchZoom()
{
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
Debug.Log(deltaMagnitudeDiff);
// ... change the orthographic size based on the change in distance between the touches.
camera.orthographicSize += deltaMagnitudeDiff * 0.05f;
// Make sure the orthographic size never drops below zero.
camera.orthographicSize = Mathf.Max(camera.orthographicSize, 10f);
}
private void Zoom()
{
camera.orthographicSize += Input.GetAxis("Mouse ScrollWheel") * 30f;
camera.orthographicSize = Mathf.Max(camera.orthographicSize, 10f);
}
private Vector2 flickstart;
private Vector2 flickend;
private float flicktime = 2;
private float flicktimer = 0;
private IEnumerator DoFlick()
{
while (flicktime >= flicktimer)
{
flicktimer += Time.deltaTime;
transform.Translate(Vector2.Lerp(flickstart, flickend, flicktime) * Time.deltaTime);
yield return new WaitForEndOfFrame();
}
}
}
I’ve also tried Lateupdate, and update but the shaking is even worse there :\