实践要求:写一个程序,实现一个完整的太阳系,其他星球围绕太阳的转速必须不一样,并且不再一个法平面内。

法平面是指过空间曲线的切点,且与切线垂直的平面。要求不在一个法平面内,则在保证所有行星以及太阳在一条轴上时,另外两条轴的比例不相同即可。公转速度在RotateAround参数里面设置。如:

这个程序在课堂程序的基础上完成,使用了预制、动态生成对象,在位置上使用Vector3定好行星初始位置,使用RotateAround设置行星公转,使用Rotate设置行星自转。参数大部分参照了太阳系的参数,如行星大小,公转速度等按比例模拟。

显示效果:


制作概述:

1.制作太阳系预制

根据太阳系各行星的大小设置Transform中的Scale

想要给白色的球贴上图的,可以去网上找到太阳系贴图,直接百度搜索就好。

然后导入图片资源,再将对应的图片拖到对应的行星即可。

最后将Sun整个拖入Assets/Resources/Perfabs(这一步是为了后续改进,直接放在Hierarchy里面后面再挂载cs文件就可以直接运行)

到这预制就做好啦~

2.开始编写代码~

使用了课程中的MVC架构

新建一个RoundSun.cs

cs文件:

2.1声明对象

    public Transform Sun;
public Transform Mercury;
public Transform Venus;
public Transform Earth;
public Transform Moon;
public Transform Mars;
public Transform Jupiter;
public Transform Saturn;
public Transform Uranus;
public Transform Neptune;
public Transform Pluto;

2.2初始行星位置

void Start () {
Sun.position = Vector3.zero;
Mercury.position = new Vector3 (, , );
Venus.position = new Vector3 (, , );
Earth.position = new Vector3 (, , );
Moon.position = new Vector3 (, , );
Mars.position = new Vector3 (, , );
Jupiter.position = new Vector3 (, , );
Saturn.position = new Vector3 (, , );
Uranus.position = new Vector3 (, , );
Neptune.position = new Vector3 (, , );
Pluto.position = new Vector3 (, , );
}

2.3设置行星公转和自转

手动设置的参数,与真实太阳系有偏差

void Update () {
Vector3 a1 = new Vector3 (, , );
Vector3 a2 = new Vector3 (, , );
Vector3 a3 = new Vector3 (, , );
Vector3 a4 = new Vector3 (, , );
Vector3 a5 = new Vector3 (, , );
Vector3 a6 = new Vector3 (, , );
Vector3 a7 = new Vector3 (, , );
Vector3 a8 = new Vector3 (, , );
Vector3 a9 = new Vector3 (, , ); Mercury.RotateAround (Sun.position, a1, *Time.deltaTime);
Mercury.Rotate (Vector3.up**Time.deltaTime); Venus.RotateAround (Sun.position, a2, *Time.deltaTime);
Venus.Rotate (Vector3.up**Time.deltaTime); Earth.RotateAround (Sun.position, a3, *Time.deltaTime);
Earth.Rotate (Vector3.up**Time.deltaTime);
Moon.transform.RotateAround (Earth.position, Vector3.up, * Time.deltaTime); Mars.RotateAround (Sun.position, a4, *Time.deltaTime);
Mars.Rotate (Vector3.up**Time.deltaTime); Jupiter.RotateAround (Sun.position, a5, *Time.deltaTime);
Jupiter.Rotate (Vector3.up**Time.deltaTime); Saturn.RotateAround (Sun.position, a6, *Time.deltaTime);
Saturn.Rotate (Vector3.up**Time.deltaTime); Uranus.RotateAround (Sun.position, a7, *Time.deltaTime);
Uranus.Rotate (Vector3.up**Time.deltaTime); Neptune.RotateAround (Sun.position, a8, *Time.deltaTime);
Neptune.Rotate (Vector3.up**Time.deltaTime); Pluto.RotateAround (Sun.position, a9, *Time.deltaTime);
Pluto.Rotate (Vector3.up**Time.deltaTime);
}

这时候直接将cs挂载到Sun里,到这里已经可以运行实现啦。


接下来实现预制,动态生成对象吧。直接放代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class FirstController : MonoBehaviour, ISceneController {
void Awake() {
Debug.Log ("load sunt...\n");
SSDirector director = SSDirector.getInstance ();
director.setFPS ();
director.currentSceneController = this;
director.currentSceneController.LoadResources ();
} public void LoadResources() {
GameObject sunset = Instantiate<GameObject> (
Resources.Load <GameObject> ("Perfabs/Sun"),
Vector3.zero, Quaternion.identity);
sunset.name = "sunset";
Debug.Log ("load sunset...\n");
}
public void Pause(){
}
public void Resume(){
}
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () { }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public interface ISceneController {
void LoadResources();
void Pause();
void Resume();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class SSDirector : System.Object {
private static SSDirector _instance; public ISceneController currentSceneController { get; set; }
public bool running{ get; set; } public static SSDirector getInstance() {
if (_instance == null) {
_instance = new SSDirector ();
}
return _instance;
} public int getFPS() {
return Application.targetFrameRate;
} public void setFPS(int fps) {
Application.targetFrameRate = fps;
}
}

在确保

在这个文件目录下

将FirstController挂载到主摄像机或者空对象即可运行

大致架构:

【Unity3D】实现太阳系的更多相关文章

  1. 用Unity3D实现太阳系仿真

    用Unity3D模拟太阳系仿真 模拟要求 写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上. 操作步骤 1.创建如下结构 sun 里包括8大行星, 并且设置好距 ...

  2. 【CityHunter】Unity3D设计AR探索模式

    为了增加游戏的乐趣性,我对项目进行了Unity3D的引入,经过一番折腾,终于做出了一个基本的AR探索模式的基本雏形. 途中的小方块就是虚拟物体,因为是静态图片,所以也不能看出什么来,只能文字形容一下: ...

  3. Unity3d学习 预设体(prefab)的一些理解

    之前一直在想如果要在Unity3d上创建很多个具有相同结构的对象,是如何做的,后来查了相关资料发现预设体可以解决这个问题! 预设体的概念: 组件的集合体 , 预制物体可以实例化成游戏对象. 创建预设体 ...

  4. Unity3d入门 - 关于unity工具的熟悉

    上周由于工作内容较多,花在unity上学习的时间不多,但总归还是学习了一些东西,内容如下: .1 根据相关的教程在mac上安装了unity. .2 学习了unity的主要的工具分布和对应工具的相关的功 ...

  5. TDD在Unity3D游戏项目开发中的实践

    0x00 前言 关于TDD测试驱动开发的文章已经有很多了,但是在游戏开发尤其是使用Unity3D开发游戏时,却听不到特别多关于TDD的声音.那么本文就来简单聊一聊TDD如何在U3D项目中使用以及如何使 ...

  6. warensoft unity3d 更新说明

    warensoft unity3d 组件的Alpha版本已经发布了将近一年,很多网友发送了改进的Email,感谢大家的支持. Warensoft Unity3D组件将继续更新,将改进的功能如下: 1. ...

  7. Unity3D框架插件uFrame实践记录(一)

    1.概览 uFrame是提供给Unity3D开发者使用的一个框架插件,它本身模仿了MVVM这种架构模式(事实上并不包含Model部分,且多出了Controller部分).因为用于Unity3D,所以它 ...

  8. Unity3D 5.3 新版AssetBundle使用方案及策略

    1.概览 Unity3D 5.0版本之后的AssetBundle机制和之前的4.x版本已经发生了很大的变化,一些曾经常用的流程已经不再使用,甚至一些老的API已经被新的API所取代. 因此,本文的主要 ...

  9. 山寨Unity3D?搜狐畅游的免费开源游戏引擎Genesis-3D

    在CSDN上看到了<搜狐畅游发布3D游戏引擎Genesis-3D 基于MIT协议开源>(http://www.csdn.net/article/2013-11-21/2817585-cha ...

随机推荐

  1. java对世界各个时区(TimeZone)的通用转换处理方法

    在进行国际性软件项目开发的过程中,有时候会碰到一些比较特殊的要求.比如:比如说,你做的是个购物网站(假设服务器放在中国上海),当全世界客户在你的网站上下订单买东西后,往往希望看到客户所在地下单时间,比 ...

  2. KCF+Opencv3.0+Cmake+Win10 测试

    配置 需要的文件下载 安装CMake,安装opencv3.0.0 在KCFcpp-master 目录下新建一个文件夹,命名为build 打开CMake-GUI配置如下: 点击Configure,编译器 ...

  3. poco时间操作

    Poco::DateTime Poco::Timespan Poco::Timestamp 时间操作 Poco::DateTime dt; //c++ 20才有 Calendar dt = dt + ...

  4. bzoj5093

    NTT+组合数学 $把每个点分别按度数考虑,由于有标号,可以得出$ $ans=n*2^{(n-1)*(n-2)}*\sum_{i=1}^{n-1}{C(n-1,i)*i^{k}}$ $本质上是求\su ...

  5. java---集合类(1)

    java.util包中包含了一系列重要的集合类.而对于集合类,主要需要掌握的就是它的内部结构,以及遍历集合的迭代模式. 接口:Collection Collection是最基本的集合接口,一个Coll ...

  6. <正则吃饺子> :关于redis配置文件参数详解

    来源于网络博文,感谢作者的分享,转载只为学习,方便查找,原文地址:http://blog.csdn.net/ljl890705/article/details/51540427 Redis是一个应用非 ...

  7. HDU 4625. JZPTREE

    题目简述:给定$n \leq 50000$个节点的数,每条边的长度为$1$,对每个节点$u$,求 $$ E_u = \sum_{v=1}^n (d(u, v))^k, $$ 其中$d(u, v)$是节 ...

  8. 正则表达式&nbsp;LINUX

    正则表达式 热身 正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等. 例如 g ...

  9. java之装箱拆箱

    参考http://how2j.cn/k/number-string/number-string-wrap/22.html 封装类 所有的基本类型,都有对应的类类型 比如int对应的类是Integer ...

  10. 2-3 Flutter开发环境与iOS开发环境设置(Mac)

    Mac下环境搭建 先不看了 都是Mac下的环境搭建