# How can I change the rotatearound radius in real time when the game is running?

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

public class TargetBehaviour : MonoBehaviour
{
// Add this script to Cube(2)
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.

public float angle; //or the speed of rotation.

public float upperLimit, lowerLimit, delay;// upperLimit & lowerLimit: heighest & lowest height;
private float height, prevHeight, time;//height:height it is trying to reach(randomly generated); prevHeight:stores last value of height;delay in radomness;

[Range(0, 50)]
[Range(0, 50)]

private void Start()
{

}

// Update is called once per frame
void Update()
{
//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.normalized, angle);
time += Time.deltaTime;
//Sets value of 'height' randomly within 'upperLimit' & 'lowerLimit' after delay
if (time > delay)
{
prevHeight = height;
height = Random.Range(lowerLimit, upperLimit);
time = 0;
}
//Mathf.Lerp changes height from 'prevHeight' to 'height' gradually (smooth transition)
transform.position = new Vector3(transform.position.x, Mathf.Lerp(prevHeight, height, time), transform.position.z);
}
}


UPDATE:

I added this to the Update:

if (radius > 0)
{
var newPos = (transform.position - Turret.transform.position).normalized * radius;
newPos += Turret.transform.position;
transform.position = newPos;
}


The problem is now that some times when the radius the distance from the Turret is small for example 20 or less some times the transform is getting right above the turret not sure why. I want it to rotate around the turret at the fixed set distance(radius) but not to be above the turret. Can't figure out why sometimes it's moving right above the turret then after some time it's getting back to the set distance.

• You mean you want to orbit in an ellipse centered at a fixed point? – DMGregory Jun 3 '18 at 23:23
• @DMGregory I mean when changing the radius the transform will get closer or more far from the Turret or any object it's rotating around. The Turret in the case is what the transform is rotating around. So if the radius is 10 the transform will rotatearound the turret at fixed distance of 10 and if the radius is 40 then at fixed distance of 40 from the turret. – Daniel Lip Jun 3 '18 at 23:58

You could have an UpdateRotation() function where you take the vector between the center object and the orbiting object, normalize it, and then find the proper location for the orbiting object by applying the changeable distance that you want on the fly. The problem is, the orbiting gameobject will just kind of snap between locations. If that's what you want, roll with that. Alternatively, you could have a coroutine run to gradually change the distance variable over time to give the game a bit of flow. A third option is implementing the Mathf.Lerp() function somehow, but off the top of my head I can't articulate how you would do that similarly to what I've already described. However, its likely going to be the cleanest option.