Untiy 实现物体围绕指定点或者某个物体旋转,可使用RotateAround()方法。
语法:
public void RotateAround(Vector3 point, Vector3 axis, float angle);
其中,point:旋转中心点位置;
axis:要围绕的轴,如x,y,z
angel:旋转的角度
该方法可以实现物体围绕指定的中心点和轴旋转。
下面举例实现围绕某个点旋转和围绕某个物体旋转。
1、围绕某个点旋转。
在场景中创建一个球体。然后编写以下脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateTest : MonoBehaviour
{
public float rotationSpeed = 0.01f;
public Transform tran;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
tran.RotateAround(new Vector3(10f,0f,0f), Vector3.up* rotationSpeed, 1f);
}
}
把脚本放到场景中,并把球体拉到tran参数中,运行场景,球体就会围绕着坐标为(10f,0f,0f),Y轴方向旋转,每帧旋转角度为1。
这里围绕的轴常用的是围绕X轴、Y轴、Z轴,表示法可参考以下:
//X轴表示法
Vector3.right
new Vector3(1,0,0)
//

文章详细介绍了Unity中如何使用RotateAround方法实现物体围绕指定点或另一个物体旋转,包括示例代码和常见轴向的表示法。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



