Hello fellow unity users -
I am making my first very simple game where a ball rolls up a hill avoiding obstacles along the way, and I wanted to integrate obstacles that move from side to side on the path. The code for it works, but only if I set the maxTransform to 100 or more, and I only want it to be like 5.
Here is the code I am using:
at first it wasn’t working, but when I made the if statement x and the else statement z it started to work side to side with only the problem I mentioned above. Any ideas? Thanks for reading
public class ObstacleMovement : MonoBehaviour
{
public float speed = 1f;
public float maxTransform = 5f;
public float minTransform = -5f;
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.z <= minTransform)
{
speed = speed * -1;
}
}
}