Help with script making enemy move up and down

My game

woila =

is like this. A 2D shooter where you control the cowboy on the left hand side and you fight against the bot/ai

As of right now he can shot and that’s good but i need him to be able to move up and down on it’s own.

If i could somehow make him be able to move up and down while like following my side while he’s shooting. But not so that he’s exactly like my controls…

I have this script for him already

using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Enemy generic behavior
/// </summary>
public class EnemyScript : MonoBehaviour
{
    private WeaponScript[] weapons;
   
    void Awake()
    {
        // Retrieve the weapon only once
        weapons = GetComponentsInChildren<WeaponScript>();
    }
   
    void Update()
    {
        foreach (WeaponScript weapon in weapons)
        {
            // Auto-fire
            if (weapon != null && weapon.CanAttack)
            {
                weapon.Attack(true);
                SoundEffectsHelper.Instance.MakeEnemyShotSound();
            }
        }
    }
}

As you can see. He’s only been scripted to shoot…
I don’t know how to make him go up and down by himself.

His location is like this y -3 at the buttom
And at the top it’s 3

So

If i could make him go up and down between those 2 locations it would be perfect…

So here comes the big question. Can you help me? please :slight_smile: this is the last thing i need.

Sincerely - Tereith -

I’m creating a new script language.

Simple

Object.Bot (move.Up.Down) = Y axis ();
Object.Bot (transistion.posistion - Y axis) = y3 -y3 ();
Object.bot (move.speed) = + 1 ();

Take that and put it onto my bot and woila BUT

nono not that easy sadly :smile:

Someone help me!!!

using UnityEngine;
using System.Collections;

public class EnemyMoveUpDown : MonoBehaviour {

int direction = 1; //int direction where 0 is stay, 1 up, -1 down
int top = 3;
int bottom = -3;

float speed = 5;

void Update ()
{
if (transform.position.y >= top)
direction = -1;

if (transform.position.y <= bottom)
direction = 1;

transform.Translate(0, speed * direction * Time.deltaTime, 0);
}
}

1 Like

put in your enemy !

2286750–153668–EnemyMoveUpDown.cs (489 Bytes)

Thanks a ton p.Trogo, i’m gonna try it out now! :slight_smile:

Works perfectly thank you so much!!!

I’m gonna save that script in my scripts folder. :slight_smile: <3 <3 :stuck_out_tongue: