Reverse Enemy Direction

hey sorry if this has been asked I tried a search but was a bit overwhelmed with what came up and didn’t really pertain to what I was looking for

I am a noob to Unity and decided to try a shot at making “Space Invaders”

I am using C# here.
I want the enemy to reach a certain point then drop and reverse direction I did this code and well didn’t do what I thought it would.
I was thinking that multiplying by a negative number it would make it change direction but it just jumps it to the other side of the screen instead

public float EnemySpeed;

    void Update()
    {
        float amountToMove = EnemySpeed * Time.deltaTime;
        transform.Translate(Vector3.right * amountToMove);

        if (transform.position.x >= 9.0f)
            transform.position = new Vector3(transform.position.x * -1, transform.position.y - 1, transform.position.z);
        else if (transform.position.x < -9.0f)
            transform.position = new Vector3(transform.position.x * -1, transform.position.y - 1, transform.position.z);
    }

would appreciate it if somebody could point me in the right direction
thanks

Shouldn’t you instead multiply Vector3.right by 1 or -1 according to where the enemy is ?

what you want to do is have one line that moves the position to the new position, then change the direction through rotation - which axis you use depends on the orientation of your game

thanks for the reply’s I will give them a try
:smile:
I will try them both out
never even thought of reversing the rotation

whether this is the best way or not i cant attest to, but if i was making this game i would orient each alien so that it was going foward 1 step each second - then when it reached the end - i would just rotate 180 and move it down 1 row

oh I doubt what I am doing is the best way or even close :stuck_out_tongue:
I only have a basic understanding of programming and am a noob to Unity.

The rotation worked and getting them to move right like you mention is my next step
again thanks :slight_smile: