Unity实现简单太阳系
資源準備
網上的貼圖資源較為散亂,此處提供一個太陽系貼圖的網站,圖片還是相當精美的:https://www.solarsystemscope.com/textures/
1、保存相應資源并導入成material:
問就是英文不好0.0(實踐下來沒有影響就完事了)
2、特別注意導入太陽material時,為了更逼真,我為其設置了自發光的屬性,實現自發光有兩種方法。
第一種:https://blog.csdn.net/qq_44148565/article/details/123117751 但是其中光的顏色還需自己調,我嘗試過后發現不太自然,于是沒有采用。
第二種:只需將太陽material的Shader屬性改為Legacy Shaders/Self-Illumin/Diffuse即可,色調比較自然。
下圖為前后對比:
但我還想要實現明顯照亮其他行星的效果,所以在太陽游戲對象中再增加了一個點光源:
效果還是很明顯的:
3、接著就是創建相應sphere,調整好其位置、大小等。初始化位置我統一設成在一條線上了,以便簡化位置參數的調整。
腳本撰寫
對于行星,要實現公轉、自轉以及公轉法平面的設置:
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Rotate : MonoBehaviour {public GameObject target; //天體旋轉的中心public float gspeed; //公轉速度public float zspeed; //自轉速度public float ry,rz; //通過y軸和z軸調整法平面 // Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){//旋轉軸Vector3 axis = new Vector3(0,ry,rz);//公轉this.transform.RotateAround(target.transform.position, axis, gspeed * Time.deltaTime);//自轉this.transform.Rotate(Vector3.up * zspeed * Time.deltaTime);} }對于太陽,只需實現自轉:
using System.Collections; using System.Collections.Generic; using UnityEngine;public class sun_rotate : MonoBehaviour {public float speed;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){this.transform.Rotate(Vector3.up * speed * Time.deltaTime);} }將腳本Rotate掛載在所有行星上,然后進行相應參數設置。以地球為例:
注意:月球的target為地球。
背景設置
背景素材在文章開頭那個網站里也找得到。教程參見:https://segmentfault.com/a/1190000008505014
運行效果
總結
以上是生活随笔為你收集整理的Unity实现简单太阳系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SSL 之数字证书
- 下一篇: 地址栏中输入网址后发生了什么?