unity3D-learing : Sun,Earth and moon

Abstract: This is a simple small program to simulate the movement of the solar system. Through this program, you will have a good understanding of the movement learning of unity3D, and the initial understanding and implementation of the movement operation.

1. Short answer combined with program verification

  • What is the nature of game object movement?
  • Please use more than three methods to realize the parabolic motion of the object. (For example, modify the Transform property, use the vector Vector3 method...)
  • Write a program to realize a complete solar system. The rotation speed of other planets around the sun must be different and not on the same normal plane.
  • Answer: The essence of the game object movement is to realize the continuous change of the game object position through the changes of the position, rotation and scale of the game object transform attribute.
  • Three ways to implement parabola: Since we know that if the parabola is in two dimensions, then its corresponding mode should be y = a * x^2 + b*x + c; (where a, b, c are all constants) If it is three-dimensional, it is spherical motion and should satisfy: x^2 + y^2 + z^2 = R^2 (where R is a constant). Here, because the focus is only to show the motion model of the parabola, the simplest model y = a * x^2 is selected. The three implementation methods are as follows:

    Modify directly through transform.position

    void Update () {
    	this.transform.position += Vector3.right * Time.deltaTime;
    	this.transform.position += 30 * Vector3.up * Time.deltaTime * Time.deltaTime;
    }
  • Modify by transform.translate

  • void  Update () {
        transform.Translate(Vector3.right * Time.deltaTime);
      transform.Translate (30 * Vector3.up * Time.deltaTime * Time.deltaTime) ;
     }

      Modified   by RigidBody

        Rigidbody rb;
	// Use this for initialization
	void Start () {
            Physics.gravity = new  Vector3 ( 0 , - 10f , 0 );//gravity acceleration
            rb = this.GetComponent<Rigidbody>();
            rb.velocity = new Vector3(0, 20, 10);
	}
	
	// Update is called once per frame
	void FixedUpdate () {
            Debug.Log(transform.position.ToString());
	}

  • 3.写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上。


    首先大致规划一下太阳系和八大行星,如果要丰富其他资源我们还可以增加流星和陨石以及丰富各个星球的轨道卫星什么的。这里我们实现1个太阳和8大行星以及一个日月模型。首先从左到右依次排开8个行星和太阳,太阳的体积增大两倍更加直观的看出是太阳。其他行星大致设置一下大小并且引入材质,这样可以更加直观的模拟太阳系的行星。之后给每个行星添加一个rotate.cs的脚本文件,让他们伴随着太阳进行转动。代码如下:

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


public class rotate : MonoBehaviour
{
    public Transform origin;//设置围绕着什么转动
    public float speed = 10; //转动的速度,可以调整
    float ry, rz;
    void Start()
    {
        ry = Random.Range(1, 360);//产生随机数,用于生成不同的轨道。
        rz = Random.Range(1, 360);//产生随机数,用于生成不同的轨道。
    }
    void Update()
    {
        Vector3 axis = new Vector3(0, ry, rz);
        this.transform.RotateAround(origin.position, axis, speed * Time.deltaTime);
    }
}
                                  

    通过修改参数我们能够实现各个星球以不同的个速度围绕着太阳以不同的轨道进行运动,之后我们为了实现日月地也在这个场景中能正常运动,我们先设置一个空对象跟地球一样的参数和运动,作为地球的虚像,之后再他底下增加一个sphere用于月球模型,同样增加rotate.cs脚本文件,以地球为中心转动,设置其他参数并且引入材质。即可实现整个模型。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325473892&siteId=291194637