How can i move object up and down in random height and random speed ?

The object is moving around another object and i want that the object that is moving in circles around the other object will also move up down random height from time to time.

So i tried this code but it’s not working yet:

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

public class MoveObjectInCircle : MonoBehaviour
{
    // Add this script to Cube(2)  
    [Header("Add your turret")]
    public GameObject Turret;//to get the position in worldspace to which this gameObject will rotate around.

    [Header("The axis by which it will rotate around")]
    public Vector3 axis;//by which axis it will rotate. x,y or z.

    [Header("Angle covered per update")]
    public float angle; //or the speed of rotation.

    // Update is called once per frame
    void Update()
    {
        StartCoroutine(MoveTo(Turret.transform.position));
        //Gets the position of your 'Turret' and rotates this gameObject around it by the 'axis' provided at speed 'angle' in degrees per update 
        transform.RotateAround(Turret.transform.position, axis, angle);
    }

    private IEnumerator MoveTo(Vector3 target)
    {
        var position = transform.position;
        while (true)
        {
            var move = 2 * Time.deltaTime;
            var dist = (position - target).sqrMagnitude;
            // if the distance we have to move is less than what is left, leave the coroutine.
            if (dist < move * move)
            {
                transform.position = target;
                yield return null;
            }

            position = Vector3.MoveTowards(position, target, dist);
            yield return 0;
        }
    }
}

You need to use the coroutine smoothly.

 private IEnumerator MoveTo(Vector3 target)
    {
        var position = transform.position;
        var cont = true;
        while (cont)
        {
            var move = moveSpeed * Time.deltaTime;
            var dist = (position - target).sqrMagnitude;

            //Debug.Log(string.Format("{0}, {1}", move * move, dist));
            // if the distance we have to move is less than what is left, leave the coroutine.
            if (dist < move * move)
            {
                transform.position = target;
                cont = false;
            }
            else
            {
                position = Vector3.MoveTowards(position, target, move);
                transform.position = position;
            }

            yield return 0;
        }
    }

Not working yet.

Two errors i got:

if (dist < move * move)
            {
                transform.position = target;
                yield return null;
            }

You can’t make just return;

return;

Cannot return a value from an iterator. Use the yield return statement to return a value, or yield break to end the iteration.

So i changed it to yield return null; but not sure if this is the right thing to do here.

Second error was on the line:

position = Vector3.MoveTowards(position, target, dist);

I needed to add the dist in the end if not it’s giving error:

position = Vector3.MoveTowards(position, target);

There is no argument given that corresponds to the required formal parameter ‘maxDistanceDelta’ of ‘Vector3.MoveTowards(Vector3, Vector3, float)’

But again not sure if giving dist there is the right thing.

And then in the end i’m calling this method like this:

StartCoroutine(MoveTo(Turret.transform.position));
[code]

But then the transform the script is attached to is now on the target and make them both the target and the transform to rotate on place.

The code in my question in my first message was wrong so i edited my question with the right code. Still not working but this is the code i wanted to work with. It’s making a gameobject to move in circles around another gameobject and i want to use the StartCoroutine to make the object move up and down in random height while moving in circles.

I don’t want it to move to the target to the turret the object in the middle.
I want it to keep moving in circles all the time and from time to time also move up and back down in random heights.
Down i mean back to the moving in circles height route.

Something like in thinking of a ball moving around another ball and from time to time it will also move up and down like bouncing(bouncing just for example) but will keep moving in circles. It should not leaving the circles moving route around the other object.

OK, so that is what I get for writing it in notepad++ and passing it off.

The corrections you made were almost correct. I re-did the code so that it worked.

Pass the null back on return, yes…

use dist on the move towards, no. You need to move an amount, not the total distance.

Tested… and runs :wink:

1 Like

Thank you very much.