日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

unity实现抛物线及太阳系

發(fā)布時間:2024/3/24 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unity实现抛物线及太阳系 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、實現(xiàn)物體的拋物線運動

1.使用translate的方法

using System.Collections; using System.Collections.Generic; using UnityEngine;public class Cylinder1 : MonoBehaviour {public float v_x;public float v_y;private float g;// Start is called before the first frame updatevoid Start(){v_x = 5;v_y = 0;g = 9.8f;}// Update is called once per framevoid Update(){v_y = g*Time.fixedDeltaTime;transform.Translate(Vector3.right* v_x * Time.fixedDeltaTime, Space.World);transform.Translate(Vector3.down * v_y * Time.fixedDeltaTime, Space.World);}}

2.使用新建Vector3的方法

using System.Collections; using System.Collections.Generic; using UnityEngine;public class Cylinder2 : MonoBehaviour {public int v_x;private float g;// Start is called before the first frame updatevoid Start(){v_x = 5;g = 9.8f;}// Update is called once per framevoid Update(){float x = this.transform.position.x + v_x*Time.deltaTime;float y = this.transform.position.y - g*Time.deltaTime*Time.deltaTime/2;this.transform.position = new Vector3(x, y, 0);}}

3.直接修改position的方法

using System.Collections; using System.Collections.Generic; using UnityEngine;public class Cylinder3 : MonoBehaviour {public int v_x;public int v_y;private float g;// Start is called before the first frame updatevoid Start(){v_x = 5;v_y = 0;g = 9.8f;}// Update is called once per framevoid Update(){this.transform.position += Vector3.right*v_x;this.transform.position += Vector3.down*(v_y + g*Time.fixedDeltaTime);} }

二、實現(xiàn)太陽系

??創(chuàng)建如下結(jié)構(gòu)并設(shè)置好各行星的大小和距離

??將行星圖片拖到對應(yīng)的行星上,在Main Camera中設(shè)置背景為黑色,初始狀態(tài)如下

??代碼如下

using System.Collections; using System.Collections.Generic; using UnityEngine;public class Rotate : MonoBehaviour {public Transform mercury;public Transform venus;public Transform earth;public Transform mars;public Transform jupiter;public Transform saturn;public Transform uranus;public Transform neptune;public Transform moon;public Transform earthclone;// Use this for initializationvoid Start () {mercury = this.transform.Find("Mercury");venus = this.transform.Find("Venus");earth = this.transform.Find("Earth");mars = this.transform.Find("Mars");jupiter = this.transform.Find("Jupiter");saturn = this.transform.Find("Saturn");uranus = this.transform.Find("Uranus");neptune = this.transform.Find("Neptune");earthclone = this.transform.Find("EarthClone");moon = earthclone.Find("Moon");}// Update is called once per framevoid Update () {mercury.RotateAround(this.transform.position, new Vector3(3, 15, 0), 47 * Time.deltaTime);mercury.Rotate(Vector3.up * Time.deltaTime * 300);venus.RotateAround(this.transform.position, new Vector3(2,10, 0), 35 * Time.deltaTime);venus.Rotate(Vector3.up * Time.deltaTime * 280);earth.RotateAround(this.transform.position, new Vector3(1, 10, 0), 30 * Time.deltaTime);earth.Rotate(Vector3.up * Time.deltaTime * 250);mars.RotateAround(this.transform.position, new Vector3(2, 15, 0), 24 * Time.deltaTime);mars.Rotate(Vector3.up * Time.deltaTime * 220);jupiter.RotateAround(this.transform.position, new Vector3(2, 10, 0), 13 * Time.deltaTime);jupiter.Rotate(Vector3.up * Time.deltaTime * 180);saturn.RotateAround(this.transform.position, new Vector3(1, 15, 0), 9 * Time.deltaTime);saturn.Rotate(Vector3.up * Time.deltaTime * 160);uranus.RotateAround(this.transform.position, new Vector3(2, 5, 0), 6 * Time.deltaTime);uranus.Rotate(Vector3.up * Time.deltaTime * 150);neptune.RotateAround(this.transform.position, new Vector3(3, 10, 0), 5 * Time.deltaTime);neptune.Rotate(Vector3.up * Time.deltaTime * 140);earthclone.RotateAround(this.transform.position, new Vector3(1, 10, 0), 30 * Time.deltaTime);moon.transform.RotateAround (earthclone.transform.position, new Vector3(1, 12, 0), 50 * Time.deltaTime);} }

??選擇組件-效果-拖尾渲染器,將時間設(shè)置為80,可以顯示行星運行軌跡。
??最終效果如下:

??可以看出,月球繞地球公轉(zhuǎn),八大行星以不同速度、在不同法平面上繞太陽公轉(zhuǎn),并且八大行星都有自轉(zhuǎn)。

視頻效果

https://www.bilibili.com/video/BV1mG411J7q6/

總結(jié)

以上是生活随笔為你收集整理的unity实现抛物线及太阳系的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。