N
N
Nikita Polishchuk2020-11-15 19:38:40
Unity
Nikita Polishchuk, 2020-11-15 19:38:40

How to implement random movement of Unity objects?

Objects are generated from one point. There is an end point. The generated objects should come to this point in random directions. If I connect a randomizer, then the generated objects constantly change the X coordinate (Y is always the same), which creates an element of object jitter. How to fix it?

using UnityEngine;

public class WatchAttack : MonoBehaviour
{
    public float Speed;
    public Vector2 Direction;
    public GameObject WatchTarget;
    float XVector;

    [System.Obsolete]
    void FixedUpdate()
    {
        RandomX();
    }

    [System.Obsolete]
    void RandomX()
    {
        XVector = Random.RandomRange(-8, 8f);
        Direction.x = XVector;
        Direction.y = WatchTarget.transform.position.y;
        transform.Translate(Speed * Direction, Space.World);
    }
}

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