博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D - 2 模拟太阳系
阅读量:6906 次
发布时间:2019-06-27

本文共 1670 字,大约阅读时间需要 5 分钟。

Unity3D - 模拟太阳系

模拟太阳系动态图(如不能显示请点进去访问)

首先是制作太阳系中的每个行星,基本上都是先创建Sophere,然后改变起始位置,添加材质和贴图,这里就不赘述了。

模拟太阳系 - 1
给每个行星创建材质包:
行星材质

之后就是创建一个行星的移动脚本使得行星绕太阳公转起来,这里需要注意的就是随机选取或者自己设一个参照轴,使得每颗行星公转的法平面不同。

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Move : MonoBehaviour {    public Transform origin;    public float speed;    float ry, rx;    // Use this for initialization      void Start()    {        speed = Random.Range(9, 12);        rx = Random.Range(10, 60);        ry = Random.Range(10, 60);    }    // Update is called once per frame      void Update()    {        this.transform.RotateAround(origin.position, new Vector3(0, rx, ry), speed * Time.deltaTime);    }}

将脚本挂载到所有行星上后所有行星就能动起来了。但是行星还不能自转,于是添加一个自转脚本挂载到所有星球上:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Rotation : MonoBehaviour {    // Use this for initialization    void Start () {            }        // Update is called once per frame    void Update () {        this.transform.RotateAround(this.transform.position, Vector3.up, Random.Range(1, 3));        }}

这时所有的行星的移动就已经搞定了,需要注意的就是月亮绕地球的旋转需要一个单独的脚本,设定以地球为旋转圆心:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Moon_Move : MonoBehaviour {    public Transform origin;    public float speed = 4;    float ry, rx;    // Use this for initialization      void Start()    {        rx = Random.Range(10, 60);//随机选取旋转轴向量        ry = Random.Range(10, 60);    }    // Update is called once per frame      void Update()    {        this.transform.RotateAround(origin.position, new Vector3(0, rx, ry), speed * Time.deltaTime);    }}

最后发现太阳系太过孤单,太空怎么能少了星海作为背景?添加一个背景板,贴上星空的图片作为背景美化一下:

星空背景

转载地址:http://wymdl.baihongyu.com/

你可能感兴趣的文章
OpenStack详细解读:定义,好处与使用实例
查看>>
俄美共同研制出新型钙钛矿太阳能电池
查看>>
黑莓Android安全补丁推送保持零延迟
查看>>
SQL Server中数据库文件的存放方式
查看>>
西部光伏电站不景气 屋顶光伏春天将至
查看>>
计算机取证如何追踪网络罪犯?
查看>>
Ignite 内存数据组织框架进入 Apache 基金会孵化
查看>>
《思科绿色数据中心建设与管理》——1.1 绿色定义
查看>>
《Linux设备驱动开发详解 A》一一
查看>>
《Windows 8 权威指南》——1.2 Windows 8平板模式下IE浏览器网页
查看>>
Ubuntu Touch 已经支持 USB Tethering 上网功能
查看>>
《人工智能:计算Agent基础》——2.7 参考文献及进一步阅读
查看>>
《iOS创意程序设计家》——第6.4节事件检测
查看>>
《数据科学:R语言实战》一1.4 问题
查看>>
《HTML5实战》——1.5 小结
查看>>
Linux管理常见错误的解决方法
查看>>
MySQL架构优化实战系列3:定时计划任务与表分区
查看>>
kafka - advertised.listeners and listeners
查看>>
Hadoop YARN学习监控JVM和实时监控Ganglia、Ambari(5)
查看>>
ECharts:免费,开源,超炫的可视化作品
查看>>