I’ve built a very simple coroutine with a emulated axis for my camera to move for a 2D-based game, however while this works, it’s not entirely what I want. It should fire until the main buttons are released, which are emulated axis in order to allow touch screen control. It only fires once, then waits for another button press.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public Transform _target;
Vector3 firstTouchPosistion;
private float horizontalControl;
private bool moving = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void axisUpdate(string dir)
{
switch(dir)
{
case "left":
horizontalControl = -1;
break;
case "right":
horizontalControl = 1;
break;
case "release":
horizontalControl = 0;
break;
}
if (!moving) {
StartCoroutine("moveCamera");
}
}
IEnumerator moveCamera()
{
moving = true;
float i = 0;
Vector3 camPosistion = transform.position;
Vector3 movePosistion = new Vector3(horizontalControl, 0, 0);
Vector3 destination = camPosistion + movePosistion;
while (i < 1)
{
i += Time.deltaTime * 5;
Debug.Log(i);
transform.position = Vector3.Lerp(camPosistion, destination, i);
yield return 0;
}
moving = false;
}
}