So im making a 2D space game where i want the enemy to start from a certian point, move one unit every second for a cycle and then reverse back to the same position in the same way
float timeElapsed;
[SerializeField] int cycleTime;
[SerializeField] int distanceTravelledPerCycle;
void Update()
{
int timeElapsedRounded = Mathf.RoundToInt(timeElapsed);
timeElapsed += Time.deltaTime;
if(timeElapsed < cycleTime)
{
if(timeElapsed >= timeElapsedRounded)
{
float xPos = transform.position.x + distanceTravelledPerCycle;
float yPos = transform.position.y;
transform.position = new Vector2(xPos, yPos);
}
}
}
This doesnt work
I want it to work like this