Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

moving objects

Discussion in '2D' started by omya76, Aug 3, 2018.

  1. omya76

    omya76

    Joined:
    Nov 28, 2017
    Posts:
    2
    Hi guys
    I'v been trying to create an array of vector 2 with random values that supposed to move a cube in random directions.
    here's what I have so far.


    int arrLen = 300;
    private Vector2[] vArr;
    public Rigidbody rb2d;


    // Use this for initialization
    void Start () {
    vArr = new Vector2[arrLen];
    this.rb2d = gameObject.AddComponent<Rigidbody>();
    for (int i = 0; i < vArr.Length; i++){
    vArr = new Vector2(Random.Range(-2, 3), Random.Range(-2, 3));
    }
    }



    // Update is called once per frame
    void Update () {
    if (Time.frameCount < arrLen)
    {
    this.rb2d.velocity = vArr[Time.frameCount];
    }

    }


    it kinda works but its still not what I want, and I'm new to unity so I'm still trying to figure out stuff
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    What is it doing?
    What do you want?
     
  3. omya76

    omya76

    Joined:
    Nov 28, 2017
    Posts:
    2
    right now its moving but not smoothly, it shakes while moving and every time I press play the cube moves more or less the same way
    this video can be a reference to how I want the cube to move(generally speaking)

    at 18:30
     
    Last edited: Aug 3, 2018
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    The way you've programmed it implies that you want it to move a bunch of random directions, but then continue moving in the final direction when the array list runs out?

    I think you'll probably be able to improve the visual if you use a coroutine loop instead of Update and wait a bit between changing directions.