Unity实现圆形旋转滚动视图
前言
一、计算圆心坐标及半径
半径公式 radius = (cellWidth * cellWidth /(4 * cellHight)) + (cellHight / 4) 圆心坐标 (0,-radius)
二、计算圆上点位置及切线和夹角
1.计算圆上点
Context.CircleCenter 是圆心位置的Transform
float angle = currentPosition * 360f;
float hudu = (angle / 180) * Mathf.PI;
Debug.Log("angle = " + angle + " hudu = " + hudu);
float xx = Context.CircleCenter.position.x + Context.Radius * Mathf.Cos(hudu);
float yy = Context.CircleCenter.position.y + Context.Radius * Mathf.Sin(hudu);
transform.position = new Vector3(xx, yy, Context.CircleCenter.position.z);
2.计算切线
代码如下(示例):
private Vector3 ClockwiseTangent(Vector3 pos, float angle)
{
float radians = ((angle) / 180f) * Mathf.PI;
Vector3 v = new Vector3(-Mathf.Cos(radians), -Mathf.Sin(radians),0) + pos;
return v - pos;
}
3.计算旋转夹角
代码如下(示例):
var tangent = ClockwiseTangent(transform.position, angle);
angleCircle = Vector2.SignedAngle(new Vector2(Context.CircleCenter.position.x, Context.CircleCenter.position.y),
new Vector2(tangent.x, tangent.y));
transform.rotation = Quaternion.Euler(0, 0, angleCircle);
三、效果
总结
向量 切线 及数学公式要温习
|