Unity3D - 2 模拟太阳系

Unity3D - 模拟太阳系

模拟太阳系动态图(如不能显示请点进去访问)

首先是制作太阳系中的每个行星,基本上都是先创建Sophere,然后改变起始位置,添加材质和贴图,这里就不赘述了。
模拟太阳系 - 1
给每个行星创建材质包:
行星材质

之后就是创建一个行星的移动脚本使得行星绕太阳公转起来,这里需要注意的就是随机选取或者自己设一个参照轴,使得每颗行星公转的法平面不同。

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

public class Move : MonoBehaviour {
    public Transform origin;
    public float speed;
    float ry, rx;
    // Use this for initialization  
    void Start()
    {
        speed = Random.Range(9, 12);
        rx = Random.Range(10, 60);
        ry = Random.Range(10, 60);
    }

    // Update is called once per frame  
    void Update()
    {
        this.transform.RotateAround(origin.position, new Vector3(0, rx, ry), speed * Time.deltaTime);
    }
}

将脚本挂载到所有行星上后所有行星就能动起来了。但是行星还不能自转,于是添加一个自转脚本挂载到所有星球上:

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

public class Rotation : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        this.transform.RotateAround(this.transform.position, Vector3.up, Random.Range(1, 3));    
    }
}

这时所有的行星的移动就已经搞定了,需要注意的就是月亮绕地球的旋转需要一个单独的脚本,设定以地球为旋转圆心:

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

public class Moon_Move : MonoBehaviour {
    public Transform origin;
    public float speed = 4;
    float ry, rx;
    // Use this for initialization  
    void Start()
    {
        rx = Random.Range(10, 60);//随机选取旋转轴向量
        ry = Random.Range(10, 60);
    }

    // Update is called once per frame  
    void Update()
    {
        this.transform.RotateAround(origin.position, new Vector3(0, rx, ry), speed * Time.deltaTime);
    }
}

最后发现太阳系太过孤单,太空怎么能少了星海作为背景?添加一个背景板,贴上星空的图片作为背景美化一下:
星空背景

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值