Y
Y
Yura Mailler2021-01-10 15:09:25
Unity
Yura Mailler, 2021-01-10 15:09:25

How to move an object by x position?

Hello! I have a script where if an enemy comes close to the player it stops and should move x but it freezes. Also, if I put not transform but Target, he just teleports to him

public Transform Target;

    public int speed;
    void Start()
    {
        Target = GameObject.FindGameObjectWithTag("Player").transform;
    }

    
    void Update()
    {
        if (transform.position.y < -3) 
        {
            transform.position = Vector2.MoveTowards(transform.position, Target.position, speed * Time.deltaTime);
        }
        else
        {
            transform.position = Vector2.MoveTowards(Target.position, transform.position, speed * Time.deltaTime);
        }
        
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question