Hi there.
I’m making a shooting range game prototype. I made a target block. What i’m trying to do is move upwards wait 3 seconds or until shot then move to position. It must be easy but geez it wasn’t. Somehow i managed to make this but i’m confused and think i’m overthinking.
public float moveSpeed = 3;
private float _speed;
private float waitTime = 3;
private float nextTime = 0;
void Awake()
{
_speed = moveSpeed;
}
void Update ()
{
float amtMove = _speed * Time.deltaTime;
transform.Translate(Vector3.up * amtMove);
if ((int)transform.position.y == 5)
{
StartCoroutine("WaitforShoot");
}
if (transform.position.y < 0)
{
StopMove();
StopCoroutine("WaitforShoot");
}
if (Input.GetKeyDown("c"))
{
StopCoroutine("WaitforShoot");
MoveDown();
}
}
void MoveUp()
{
_speed = moveSpeed;
}
void StopMove()
{
_speed = 0;
}
public void MoveDown()
{
_speed = -moveSpeed;
}
IEnumerator WaitforShoot()
{
StopMove();
yield return new WaitForSeconds(3);
MoveDown();
}
}