Unity implements a simple solar system

Resource preparation

The texture resources on the Internet are relatively scattered. Here is a website with solar system textures. The pictures are quite beautiful: https://www.solarsystemscope.com/textures/

1. Save the corresponding resources and import them into material:

Insert image description here
The question is because my English is not good 0.0 (it will be fine if there is no impact in practice)

2. Pay special attention when importing the sun material. In order to be more realistic, I set the self-illumination attribute for it. There are two methods to achieve self-illumination.
The first one: https://blog.csdn.net/qq_44148565/article/details/123117751 However, the color of the light needs to be adjusted by yourself. After trying it, I found that it was not natural, so I did not use it.
Second type: Just change the Shader attribute of the sun material to Legacy Shaders/Self-Illumin/Diffuse, and the tone will be more natural.
The picture below shows the before and after comparison:

before fixing
After modification

But I also wanted to achieve the effect of clearly lighting up the other planets, so I added another point light to the sun game object:

Insert image description here

The effect is still very obvious:

Insert image description here
3. The next step is to create the corresponding sphere and adjust its position, size, etc. I set the initial position to be on a line to simplify the adjustment of position parameters.

Insert image description here

Scripting

For planets, it is necessary to realize the setting of revolution, rotation and revolution normal plane:

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);
    }
}

For the sun, just implement rotation:

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);
    }
}

Mount the script Rotate on all planets, and then set the corresponding parameters. Take the earth as an example:

Insert image description here
Note: The target of the moon is the earth.


Background settings

The background material can also be found on the website at the beginning of the article. For tutorials, see: https://segmentfault.com/a/1190000008505014

running result

Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/qq_56266553/article/details/127464914