unity3d学习之Rotate和RotateAround做地球绕太阳旋转

视频教学来源:www.51cto.com


新建3个Sphere,分别名为sun,earth,moon,

新建2个C#script,分别名为Rotatescr,RotateAroundscr

Rotatescr在update()中只写一句

this.transform.Rotate (Vector3.up*1F,Space.World);

RotateAroundscr也在upate()中写一句

this.transform.RotateAround (RotateAroundObj.position,Vector3.up,1F);

public Transform RotateAroundObj是被环绕的对象。
分别拖拽Rotatescr给3个Sphere,

拖拽RotateAroundscr给earth和moon

最后把sun拖拽给earth的RotateAroundscr中的RotateAroundObj,把earth拖拽给moon的RotateAroundscr中的RotateAroundObj。

目前在初学阶段,只知道怎么做,至于为什么,以后加深理解。

### Unity 中 `Rotate` `RotateAround` 方法的区别及用法 #### `Transform.Rotate` 此方法用于围指定轴旋转游戏对象。可以基于世界坐标系或本地坐标系执行旋转操作。 - **参数** - `xAngle`, `yAngle`, `zAngle`: 各轴的角度。 - `relativeTo`(可选): 默认为 Space.Self 表示相对于自身,也可以设置为空间变换Space.World表示相对世界空间[^1]。 ```csharp // Example of using Transform.Rotate with local space rotation. transform.Rotate(0, Time.deltaTime * speed, 0); ``` 该函数非常适合处理物体自身的姿态调整,比如让角色转向特定方向或者使物品自旋等场景下非常有用。 #### `Transform.RotateAround` 相比之下,这个功能允许开发者定义一个外部点作为中心来圆周运动。这有助于模拟行星环恒星之类的物理现象或是创建更复杂的动画效果。 - **参数** - `point`: 定义要之转动的固定位置向量(Vector3)。 - `axis`: 描述旋转平面的方向矢量。 - `angle`: 应用到目标上的角度增量值。 ```csharp // Demonstrates how to make an object orbit around another point in world space. Vector3 centerPoint = new Vector3(5f, 0f, 5f); // Center of the circle path float angleIncrementPerSecond = 90.0F; // Degrees per second transform.RotateAround(centerPoint, Vector3.up, Time.deltaTime * angleIncrementPerSecond); ``` 通过上述代码片段可以看出,在实现某些特殊类型的交互式行为时,如卫星轨道、车轮滚动等情况下,`RotateAround` 提供了一种更为直观的方式去构建此类逻辑[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值