//旋转速度
float RevolutionSpeed = 20f;
//中心点
public Transform RevolutionTarget;
//点所在的角度
public float _Angle = 0;
//X,y,z轴的位置偏移
public float XDeviation;
public float YDeviation;
public float ZDeviation;
//半径
public float _radius;
//x,y,z轴的膨胀偏移
public float XRotateSpeed;
public float YRotateSpeed;
public float ZRotateSpeed;
//上面出的是一个平面的圆,加上这个圆或者椭圆即会倾斜
public float An;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//自我旋转
_Angle += RevolutionSpeed * Time.deltaTime;
if (_Angle > 360) _Angle -= 360;
//椭圆算法
float x = RevolutionTarget.position.x + XDeviation + Mathf.Cos(_Angle * (Mathf.PI / 180)) * _radius * XRotateSpeed;
float y = RevolutionTarget.position.y + YDeviation + Mathf.Sin(_Angle * (Mathf.PI / 180)) * _radius * YRotateSpeed;
float z = RevolutionTarget.position.z + ZDeviation + Mathf.Sin(_Angle * (Mathf.PI / 180)) * _radius * ZRotateSpeed;
//椭圆算法的基础上添加倾斜算法,如无需倾斜注释掉下面两行代码
x = x * Mathf.Cos(An) + y * Mathf.Sin(An);
y = x * -Mathf.Sin(An) + y * Mathf.Cos(An);
transform.position = new Vector3(x, y, z);
//此时图片在后方,物体越后面,物体越来越小,且越来越暗淡,这个判断值需要比较圆形图片在最左最右时的位置,
//这里根据我的需求,通过判断Y值,判定图片在后方还是前方
if (transform.localPosition.y > 145)
{
float posy = transform.localPosition.y;
float s = 1f - (0.5f * (posy / 200f));
transform.localScale = new Vector3(s, s, 1f);
// transform.GetComponent<Image>().color = new Color(255f, 255f, 255f, s);
}
//此时图片在前方,物体越前面,越来越大,越来越亮
if (transform.localPosition.y <155)
{
float posy = transform.localPosition.y;
float s = 1f - (0.5f * (posy / 200f));
transform.localScale = new Vector3(s, s, 1f);
//transform.GetComponent<Image>().color = new Color(255f, 255f, 255f, s);
}
}
private void OnDrawGizmos()
{
// 绘制目标线
Gizmos.color = Color.red;
Gizmos.DrawLine(RevolutionTarget.position, transform.position);
// 绘制轨道
Gizmos.color = Color.blue;
float sub = 0.1f;
float i = 0;
// 轨道起点
float x = RevolutionTarget.position.x + XDeviation + _radius * Mathf.Cos(i) * XRotateSpeed;
float y = RevolutionTarget.position.y + YDeviation + _radius * Mathf.Sin(i) * YRotateSpeed;
float z = RevolutionTarget.position.z + ZDeviation + _radius * Mathf.Sin(i) * ZRotateSpeed;
x = x * Mathf.Cos(An) + y * Mathf.Sin(An);
y = x * -Mathf.Sin(An) + y * Mathf.Cos(An);
Vector3 sPoint = new Vector3(x, y, z);
Vector3 ePoint = Vector3.zero;
for (i += sub; i <= Mathf.PI * 2; i += sub)
{
x = RevolutionTarget.position.x + XDeviation + _radius * Mathf.Cos(i) * XRotateSpeed;
y = RevolutionTarget.position.y + YDeviation + _radius * Mathf.Sin(i) * YRotateSpeed;
z = RevolutionTarget.position.z + ZDeviation + _radius * Mathf.Sin(i) * ZRotateSpeed;
x = x * Mathf.Cos(An) + y * Mathf.Sin(An);
y = x * -Mathf.Sin(An) + y * Mathf.Cos(An);
ePoint = new Vector3(x, y, z);
Gizmos.DrawLine(sPoint, ePoint);
sPoint = ePoint;
}
// 轨道终点
x = RevolutionTarget.position.x + XDeviation + _radius * Mathf.Cos(Mathf.PI * 2) * XRotateSpeed;
y = RevolutionTarget.position.y + YDeviation + _radius * Mathf.Sin(Mathf.PI * 2) * YRotateSpeed;
z = RevolutionTarget.position.z + ZDeviation + _radius * Mathf.Sin(Mathf.PI * 2) * ZRotateSpeed;
x = x * Mathf.Cos(An) + y * Mathf.Sin(An);
y = x * -Mathf.Sin(An) + y * Mathf.Cos(An);
ePoint = new Vector3(x, y, z);
Gizmos.DrawLine(sPoint, ePoint);
}