from point A to point B with random movement

Hi, is there a way ( or is there a script ) to move an object from point A to point B with random movement(like an insect)?

Thanks in advance!

There are any number of ways you could achieve this - it’s the kind of open-ended question that might make a good assignment for a beginner coding task!

Here’s one way that springs to mind:

  1. Mark out N number of points from a straight line from A to B.
  2. Move each point a random amount, perhaps by a distance no greater than the distance of A->B divided by N.
  3. Interpolate your insect along each of these points, which should now give a random zig-zagging route from A-B.

Another method:

  1. Start your insect facing within 90 degrees of the target
  2. move your insect forward at a constant speed.
  3. allow your insect’s direction to sway (perhaps using Mathf.Sin and Time.time) between -N and N degrees of the target direction, where N decreases to zero as the distance to the target approaches zero.

hope these give some inspiration!

Thanks duck, this will help!

someone can tell me why this little piece of simple code doesn’t work?

protected void Update()
    {
        if (c)
        {
            rVal = UnityEngine.Random.Range(-45f, 45f);
            c = false;
            oldQ = transform.localRotation;
        }
        transform.rotation = Quaternion.Lerp(oldQ, Quaternion.Euler(0, rVal, 0), Time.time * RotationSpeed);
    }

it only work the first time i lunch it, then the interpolation stop.

It’s because you’re using Time.time instead of Time.deltaTime.

Time.time gives your the number of seconds elapsed since the project started playing.

Time.deltaTime gives you the amount of time elapsed since the last frame.

To get useful results from any Lerp command, you need to feed in values from 0 to 1 as the 3rd parameter. Lerp returns a value somewhere between the Min and Max, depending on where between 0 and 1 the 3rd parameter is.

If you use Time.time, your values will quickly exceed 1, and therefore the output is just clamped at the maximum value.

Hope this helps you sort it out.

:shock: how could i be so idiot! i didn’t notice the Time.time sorry.thanks for sharing your time again duck. :sweat_smile:

Also, search UnitySteer and take a look at the Wander steering method.