Unity3d--实现太阳系仿真

一.实验要求

写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。

二.实验过程

  • 创建如下结构 solar 里包括太阳和8大行星, 并且设置好距离和大小
    在这里插入图片描述
  • 在网上找到相应贴图 添加到assets
    在这里插入图片描述将贴图拖动到对应星球的名字上即可。
    在这里插入图片描述
  • 创建c#脚本 使每个行星绕太阳转

围绕自身的旋转,参数为旋转轴 * 速度,注意只有一个参数

    this.transform.Rotate (Vector3.up * rotaSpeed);

围绕某个点的旋转,参数为围绕的点,旋转轴,速度

this.transform.RotateAround (Vector3.zero, Vector3.up, rotaSpeed);

首先脚本中设置星球对应的transform:

public Transform sun;
public Transform mercury;
public Transform venus;
public Transform earth;
public Transform mars;
public Transform jupiter;
public Transform saturn;
public Transform uranus;
public Transform neptune;
public Transform moon;
public Transform earthclone;

通过public transform来找到各个球体 然后通过调用RotateAround()设置公转,通过调用Rotate()方法设置自转。

		mercury.RotateAround(this.transform.position, new Vector3(3, 15, 0), 47 * Time.deltaTime);
		mercury.Rotate(Vector3.up * Time.deltaTime * 300);
		venus.RotateAround(this.transform.position, new Vector3(2,10, 0), 35 * Time.deltaTime);
		venus.Rotate(Vector3.up * Time.deltaTime * 280);
		earth.RotateAround(this.transform.position, new Vector3(1, 10, 0), 30 * Time.deltaTime);
		earth.Rotate(Vector3.up * Time.deltaTime * 250);
		mars.RotateAround(this.transform.position, new Vector3(2, 15, 0), 24 * Time.deltaTime);
		mars.Rotate(Vector3.up * Time.deltaTime * 220);
		jupiter.RotateAround(this.transform.position, new Vector3(2, 10, 0), 13 * Time.deltaTime);
		jupiter.Rotate(Vector3.up * Time.deltaTime * 180);
		saturn.RotateAround(this.transform.position, new Vector3(1, 15, 0), 9 * Time.deltaTime);
		saturn.Rotate(Vector3.up * Time.deltaTime * 160);
		uranus.RotateAround(this.transform.position, new Vector3(2, 5, 0), 6 * Time.deltaTime);
		uranus.Rotate(Vector3.up * Time.deltaTime * 150);
		neptune.RotateAround(this.transform.position, new Vector3(3, 10, 0), 5 * Time.deltaTime);
		neptune.Rotate(Vector3.up * Time.deltaTime * 140);

最后思考如何实现月球绕着地球转,现在运行发现月球旋转速度与地球自转速度相同,这是不正常的,其原因是地球的自转影响了月球的公转, 月球是相对一个自转的地球来的,月球公转叠加了地球自转。
所以我们需要添加一个空的游戏对象earthclone,且大小和位置都和地球相同,然后对earthclone设置和Earth一样的公转,但是不设置自转,把Moon放到earthclone里让Moon相对与earthclone公转,这样就能解决问题。

	earthclone.RotateAround(this.transform.position, new Vector3(1, 10, 0), 30 * Time.deltaTime);
	moon.transform.RotateAround (earthclone.transform.position, new Vector3(1, 12, 0), 50 * Time.deltaTime);

最终的目录结构:
在这里插入图片描述
现在我们来添加行星运行轨道,点击Component->Effects->Trail Renderer就可以在每个行星运行之后留下一道线,接着我们修改这条线的持续时间
在这里插入图片描述
将Time修改为20就可以实现效果。
最后修改camera的属性使其能够立体的看整个太阳系。调整background为黑色使其更像太空。
在这里插入图片描述

三.实验结果

在这里插入图片描述
github地址:SolarSystem
视频连接:太阳系仿真

  • 3
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值