- Home /
How can i set a radius for rotatearound ?
Now i'm using the variable radius just to move the transform at the first time when running the game to the radius position and then the transform is rotatearound on the radius.
I'm getting the radius value from another script i'n calling it dc.xRadius.
When i'm changing the radius in the other script the xRadius the transform in this script will move to the new radius position and then will rotatearound on this radius.
but in this part in the script i'm using the local variable radius because dc is null :
else
{
if (lastRadius != radius)
{
move = true;
lastRadius = radius;
}
}
but the transform is keep rotatearound in big radius it's not changing to 0 or if i set the radius in the inspector at runtime to 1 or to 20 the transform is not moving to the new radius and i used a break point and i see that the value of radius is 0 or when i changed to 1 or 10 but the transform is not moving to the next radius.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateAroundTarget : MonoBehaviour
{
public Transform target;
public float rotatingSpeed;
public float movingSpeed;
public Vector3 axis;
public bool randomHeight;
public float setRandomHeight = 1;
public float radius;
public DrawCircle dc;
private float lastRadius;
private bool move = false;
private float t = 0.0f;
public float upperLimit, lowerLimit, delay;
private float prevHeight;
private Vector3 radiusPosition;
private void Start()
{
if (dc != null)
{
lastRadius = dc.xRadius;
}
else
{
lastRadius = radius;
}
move = true;
}
private void Update()
{
if (dc != null)
{
radiusPosition = new Vector3(target.position.x, target.position.y, dc.xRadius);
}
else
{
radiusPosition = new Vector3(target.position.x, target.position.y, radius);
}
if (move == false)
{
transform.RotateAround(target.position, axis, rotatingSpeed * Time.deltaTime);
t += Time.deltaTime;
if (t > delay)
{
prevHeight = setRandomHeight;
setRandomHeight = Random.Range(lowerLimit, upperLimit);
t = 0;
}
var tt = transform.position;
tt.y = Mathf.Lerp(prevHeight, setRandomHeight, t);
transform.position = tt;
}
if (dc != null)
{
if (lastRadius != dc.xRadius)
{
move = true;
lastRadius = dc.xRadius;
}
}
else
{
if (lastRadius != radius)
{
move = true;
lastRadius = radius;
}
}
if (move)
{
if (transform.position != radiusPosition)
{
float step = movingSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position,
radiusPosition, step);
}
else
{
move = false;
}
}
}
}