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 update
    void Start(){
    
    }
    
    // Update is called once per frame
    void 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 update
    void Start(){
    
    }
    
    // Update is called once per frame
    void Update()
    {
    
    
        this.transform.Rotate(Vector3.up * speed * Time.deltaTime);
    }
}

将脚本Rotate挂载在所有行星上,然后进行相应参数设置。以地球为例:

在这里插入图片描述
注意:月球的target为地球。


背景设置

背景素材在文章开头那个网站里也找得到。教程参见:https://segmentfault.com/a/1190000008505014

运行效果

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_56266553/article/details/127464914