How do I make enemy switch the other way when bouncing off collider?

I am trying to make a top down game where the enemy continuously moves up and down. This is my code so far.

using UnityEngine;
using System.Collections;

public class EnemyRandomMovement : MonoBehaviour {

// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
transform.position = new Vector3 (transform.position.x, transform.position.y - 0.1f, 0f);
}
}

I’m actually unsure what to do now. (I’m a beginner)
I just need the enemy to bounce off the background box collider and switch directions.

Please use the “Insert code” option when you post code on the forums. Or use code tags. There is a pinned thread on this, for more details :slight_smile:

As for your situation, I’m pretty sure that you could put a kinematic rigidbody on your enemy (or the backgroundbox) along with a trigger collider and use the OnTriggerEnter callback to switch the enemy’s direction.

This is a tutorial on colliders as triggers: 3D Physics - Unity Learn