J
J
Jestome2018-11-17 21:38:18
Unity
Jestome, 2018-11-17 21:38:18

How to specify enemy movement trajectory in Unity 2d?

Toasters, do not scold strictly, Unity only opened yesterday (I used to write in python at all). In general, you need a script so that the enemy moves from one Transform to another and stops in their coordinates for a while (namely 2D). And please don’t write “learn to google” or something else (Google gives out complex 3D scripts, for example, with chasing a player, but I don’t need it (well, or, probably, I don’t know how to google)) ), in my defense I can say that Unity is familiar with the second day. Thanks a lot.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jestome, 2018-11-17
@Jestome

Already wrote who needs to use:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController2D : MonoBehaviour
{

    public int Speed = 1;
    public Transform point_1;
    public Transform point_2;
    Rigidbody2D rgb;
    bool OnRight;

    void Awake()
    {
        rgb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (gameObject.transform.position.x <= point_2.position.x)
        {
            OnRight = true;
        }
        if (gameObject.transform.position.x >= point_1.position.x)
        {
            OnRight = false;
        }

        MakePosition();
    }

    void MakePosition()
    {
        if (OnRight)
        {
            rgb.velocity = new Vector2(Speed, rgb.velocity.y);
        }
        else
        {
            rgb.velocity = new Vector2(-Speed, rgb.velocity.y);
        }
    }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question