Hey dear experts,
I have some issues with my logic by moving an Obstacle for my Game. Basically, the Obstacle should move from left to right until it hits the end of the screen, and then it should change his direction and go from right to left until it hits the left end of the screen. I hope this is described well.
However, my conditions now are doing this:
It moves in the right direction until it hits the right end of the screen
changes direction
moves infinitely in the left direction
Here’s the code, hope you can help me out!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Barrierhandler : MonoBehaviour {
public float speed;
public float maxTransform;
public float minTransform;
void Update(){
transform.Translate(speed * Time.deltaTime, 0, 0);
checkDirection();
}
public void checkDirection(){
if(transform.position.x < maxTransform){
speed = speed * 1;
}
else if(transform.position.x > maxTransform){
speed = speed * -1;
}
else if(transform.position.x <= minTransform){
speed = speed *1;
}
}
}