unity实现抛物线及太阳系
生活随笔
收集整理的這篇文章主要介紹了
unity实现抛物线及太阳系
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、實現物體的拋物線運動
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);} }二、實現太陽系
??創建如下結構并設置好各行星的大小和距離
??將行星圖片拖到對應的行星上,在Main Camera中設置背景為黑色,初始狀態如下
??代碼如下
??選擇組件-效果-拖尾渲染器,將時間設置為80,可以顯示行星運行軌跡。
??最終效果如下:
??可以看出,月球繞地球公轉,八大行星以不同速度、在不同法平面上繞太陽公轉,并且八大行星都有自轉。
視頻效果
https://www.bilibili.com/video/BV1mG411J7q6/
總結
以上是生活随笔為你收集整理的unity实现抛物线及太阳系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ElasticSearch实战
- 下一篇: 测风雷达matlab,雷达数字中频接收机