- Home /
How can I fix this Coroutine Camera?
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;
}
}
I'm sorry but I dont understand what exactly you are asking.
The coroutine -should- repeat until the variable horizontalControl changes. However, it only fires once. The button itself fires twice, once when it's pressed which changes horizontalControl to the proper direction, and once when it's released changing it back to zero.
Answer by robertbu · Jun 03, 2013 at 03:54 PM
Even with your comment, I have to guess a bit on what you want to do. If I'm right, I'd remove the coroutine and do something like:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public Transform _target;
Vector3 firstTouchPosistion;
private float horizontalControl;
private Vector3 destination;
private float speed = 0.5f;
void Start () {
destination = transform.position;
}
// Update is called once per frame
void Update () {
transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);
}
public void axisUpdate(string dir)
{
switch(dir)
{
case "left":
horizontalControl = -1.0f;
break;
case "right":
horizontalControl = 1.0f;
break;
case "release":
horizontalControl = 0.0f;
break;
}
destination = transform.position + new Vector3(horizontalControl, 0.0f, 0.0f);
}
}
NICE! That works and produces FAR smoother results in camera movement then IEnumerator. Still a bit jerky, but far better then transform.position += or transform.Translate() on Android. I think I can ignore this now, aside from telling it to occasionally point at the character and follow them ins$$anonymous$$d of under player control.
Basically, I've been trying to solve the issue of a jerky 2D-limited camera on the Android platform that seems to be occuring for almost everyone.
If you replace Vector3.$$anonymous$$oveTowards() with Vector3.Lerp() you will get an eased movement. You might also try changing Update() to LateUpdate().
Your answer
Follow this Question
Related Questions
Control the camera with a half of the touch screen 0 Answers
Button isn't working 1 Answer
How can I rotate a camera useing 2 touches? 0 Answers
Move to touch position 1 Answer
Dragging Camera based on Touch 0 Answers