Unity实现简易行星模拟

Unity实现简易行星模拟

准备

VS2019
Unity3D
贴图素材

实现

1.新建Unity项目
2.在assets中建script文件夹,material文件夹。
2.创造9个sphere物体:太阳,水星,金星,地球,火星,木星,土星,天王星,海王星。
3.将素材贴图导入Assets-material中,对应将行星贴图。
4.参照各行星大小,自转速度,公转速度编写代码。
5.将代码文件放到摄影机上,分别把行星物体放到对象上。

效果展示

Unity实现简易行星模拟_第1张图片

参考数据

Unity实现简易行星模拟_第2张图片

代码

// using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveRotate : MonoBehaviour {
	public GameObject Sun;//太阳
	public GameObject Mercury;//水星
	public GameObject Venus;//金星
	public GameObject Earth;//地球
	public GameObject Mars;//火星
	public GameObject Jupiters;//木星
	public GameObject Saturn;//土星
	public GameObject Uranus;//天王
	public GameObject Neptune;//海王
	public GameObject tuxinghuan;//土星环
	public float Sun_RotationSpeed;
	public float Mercury_RotationSpeed;
	public float Venus_RotationSpeed;
	public float Earth_RotationSpeed;
	public float Mars_RotationSpeed;
	public float Jupiters_RotationSpeed;
	public float Saturn_RotationSpeed;
	public float Uranus_RotationSpeed;
	public float Neptune_RotationSpeed;
   //定义自转速度
	
	void Start () {
		
	}
	
	
	void Update () {
		tuxinghuan.transform.RotateAround(Saturn.transform.position, Saturn.transform.up, 1f);
        Mercury.transform.RotateAround(Sun.transform.position, Sun.transform.up, 2.35f);
        Venus.transform.RotateAround(Sun.transform.position, Sun.transform.up, 1.75f);
        Earth.transform.RotateAround(Sun.transform.position, Sun.transform.up, 1.5f);
        Mars.transform.RotateAround(Sun.transform.position, Sun.transform.up, 1.2f);
        Jupiters.transform.RotateAround(Sun.transform.position, Sun.transform.up, 0.65f);
        Saturn.transform.RotateAround(Sun.transform.position, Sun.transform.up, 0.23f);
        Uranus.transform.RotateAround(Sun.transform.position, Sun.transform.up, 0.34f);
        Neptune.transform.RotateAround(Sun.transform.position, Sun.transform.up, 0.27f);
        //实现绕太阳自转
        Sun.transform.Rotate(Vector3.down * Sun_RotationSpeed, Space.World);
        Mercury.transform.Rotate(Vector3.down * Mercury_RotationSpeed, Space.World);
        Venus.transform.Rotate(Vector3.down * Venus_RotationSpeed, Space.World);
        Earth.transform.Rotate(Vector3.down * Earth_RotationSpeed, Space.World);
        Mars.transform.Rotate(Vector3.down * Mars_RotationSpeed, Space.World);
        Jupiters.transform.Rotate(Vector3.down * Jupiters_RotationSpeed, Space.World);
        Saturn.transform.Rotate(Vector3.down * Saturn_RotationSpeed, Space.World);
        Uranus.transform.Rotate(Vector3.down * Uranus_RotationSpeed, Space.World);
        Neptune.transform.Rotate(Vector3.down * Neptune_RotationSpeed, Space.World);
        //实现自转



    }
}

以上

你可能感兴趣的:(Unity实现简易行星模拟)