All I want is to create a 2d game in which a button is pressed, the camera will move at a certain distance and then stop. For example when I press right, the camera will move towards the right and will stop after moving 10f
.
this is what my code looks like. Is there a better solution since this doesn’t work 100% of the time.
public GameObject cam;
public float speed;
void Update()
{
if(moveR == true)
{
cam.transform.Translate(speed * Time.deltaTime, 0, 0);
StartCoroutine(ghost());
}
if(moveL == true)
{
cam.transform.Translate(-speed * Time.deltaTime, 0, 0);
StartCoroutine(ghost());
}
if(moveU == true)
{
cam.transform.Translate(0, speed * Time.deltaTime, 0);
StartCoroutine(ghost());
}
if(moveD == true)
{
cam.transform.Translate(0, -speed * Time.deltaTime, 0);
StartCoroutine(ghost());
}
}
IEnumerator ghost()
{
yield return new WaitForSeconds(0.2f);
moveR = false;
moveL = false;
moveU = false;
moveD = false;
}
public void moveRight()
{
moveR = true;
}
public void moveLeft()
{
moveL = true;
}
public void moveUp()
{
moveU = true;
}
public void moveDown()
{
moveD = true;
}