How to make the transform rotate around target with random height ?

When the object rotate around the target make the transform change the height randomly every each X seconds?

For example make that each time take the transform to change to the new random height X duration time then stay at that height Y duration time in seconds then change to the next random height.

Now the transform change height’s but never rotate around anymore.
I want it to rotate around the target and change height randomly at the same time.

I tried to do it with this part in the script :

Vector3 radiusPosition = new Vector3(target.position.x, Mathf.Lerp(0, 
            target.position.y + setRandomHeight, t), dc.xRadius);

        t += 0.1f * Time.deltaTime;
        if(t >= 1.0f)
        {
            setRandomHeight = Random.Range(0, setRandomHeight);
            t = 0.0f;
        }

but it’s not duration time and not what i wanted and it’s not rotating around anymore it’s just staying at place and only change the height randomly.

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

public class RotateAroundTarget : MonoBehaviour
{
    public Transform target;
    public float speed;
    public Vector3 axis;
    public bool randomHeight;
    public float setRandomHeight = 1;
    public DrawCircle dc;

    private float lastRadius;
    private bool move = false;
    private float t = 0.0f;

    private void Start()
    {
        lastRadius = dc.xRadius;
        move = true;
       
        if(randomHeight)
        {
            setRandomHeight = Random.Range(0, setRandomHeight);
        }
    }

    private void Update()
    {
        Vector3 radiusPosition = new Vector3(target.position.x, Mathf.Lerp(0, 
            target.position.y + setRandomHeight, t), dc.xRadius);

        t += 0.1f * Time.deltaTime;
        if(t >= 1.0f)
        {
            setRandomHeight = Random.Range(0, setRandomHeight);
            t = 0.0f;
        }

        if (move == false)
        {
            transform.RotateAround(target.position, axis, speed * Time.deltaTime);
        }

        if (lastRadius != dc.xRadius)
        {
            move = true;
           
            lastRadius = dc.xRadius;
        }

        if(move)
        {
            if (transform.position != radiusPosition)
            {
                float step = 1 * Time.deltaTime;
                transform.position = Vector3.MoveTowards(transform.position,
                    radiusPosition, step);
            }
            else
            {
                move = false;
            }
        }
    }
}

Probably want to use two Transforms, one parented below the other:

  • one transform that you set the .localPosition Y to the height you want periodically (See below for smoothness)

  • one transform below that which only rotates around the way you want it to.

This is an easy way to tween to different arbitrary heights:

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

If that doesn’t suit you, try a package like DOTween or LeanTween.