創建好一個Unity3D專案,名字隨意,
并創建一個名為Roteta的腳本,以下為此腳本的代碼,復制替換腳本中的代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Roteta : MonoBehaviour
{
public GameObject Sun; //太陽
public GameObject Mercury; //水星
public GameObject Venus; //金星
public GameObject Earth; //地球
public GameObject Mars; //火星
public GameObject Jupiter; //木星
public GameObject Saturn; //土星
public GameObject Uranus; //天王星
public GameObject Neptune; //海王星
void Update()
{
Mercury.transform.RotateAround(Sun.transform.position, Sun.transform.up, 1.5f);
Venus.transform.RotateAround(Sun.transform.position, Sun.transform.up, 1.25f);
Earth.transform.RotateAround(Sun.transform.position, Sun.transform.up, 1f);
Mars.transform.RotateAround(Sun.transform.position, Sun.transform.up, 0.8f);
Jupiter.transform.RotateAround(Sun.transform.position, Sun.transform.up, 0.7f);
Saturn.transform.RotateAround(Sun.transform.position, Sun.transform.up, 0.6f);
Uranus.transform.RotateAround(Sun.transform.position, Sun.transform.up, 0.5f);
Neptune.transform.RotateAround(Sun.transform.position, Sun.transform.up, 0.4f);
//公轉
Mercury.transform.Rotate(Vector3.down * 0.5f, Space.World);
Venus.transform.Rotate(Vector3.down * 0.2f, Space.World);
Earth.transform.Rotate(Vector3.down * 0.8f, Space.World);
Mars.transform.Rotate(Vector3.down * 0.85f, Space.World);
Jupiter.transform.Rotate(Vector3.down * 1.3f, Space.World);
Saturn.transform.Rotate(Vector3.down * 1.2f, Space.World);
Uranus.transform.Rotate(Vector3.down * 1.1f, Space.World);
Neptune.transform.Rotate(Vector3.down * 1.15f, Space.World);
//自轉
}
}
這是公轉與自轉的相關代碼
在unity中創建9個圓(Sphere)給它們上顏色設定好大小以方便分別,效果想再明顯的話可以再在每個素材球上加上圖片,

設定好后創建一個Gameobject,掛載最上方的代碼,并將圓(Sphere)依次賦給代碼腳本的變數中
完成以上操作運行下程式看看
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/287002.html
標籤:其他
