思路:月亮绕着地球转,地球绕着太阳转
在Hierarchy右键创建三个3D 球型物体:Sphere
并且以这种层级关系,分别命名 选定后F2快捷命名
同时稍微改变Scene视图中,他们的位置
然后在Project层级Assets右键创建脚本文件夹Scripts
在里面创建两个 旋转脚本分别命名为 Rotate Rotate1
让我们先编写月亮:公转、自转脚本Rotate
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
//月球自转速度
private float Rotationspeed = 100.0f;
//月球公转速度
private float Revolutionspeed = 100.0f;
public Transform earth;
void Start()
{
}
void Update()
{
//先写自传
transform.Rotate(Vector3.up * Rotationspeed * Time.deltaTime);
//再写公转
//transform.RotateAround(Vector3.zero, Vector3.up, Revolutionspeed * Time.deltaTime);
transform.RotateAround(earth.position, Vector3.up, Revolutionspeed * Time.deltaTime);
}
}
再编写地球:公转、自转脚本Rotate1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate1 : MonoBehaviour
{
//定义地球自转速度
private float Rotationspeed1 = 10.0f;
//定义地球公转速度
private float Revolutionspeed1 = 10.0f;
public Transform sun1;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(Vector3.up * Rotationspeed1 * Time.deltaTime);
//transform.RotateAround(Vector3.zero, Vector3.up, Revolutionspeed1 * Time.deltaTime);
transform.RotateAround(sun1.position, Vector3.up, Revolutionspeed1 * Time.deltaTime);
}
}
然后我们分别将Rotate、Rotate1 脚本分别拖给Moon 和Earth
我们就实现了球体围绕旋转的功能啦!
课后练习:
自学Materials材质是什么东西,怎么运用
然后给太阳和月亮添加材质,使他们换色