小球自动围绕旋转

思路:月亮绕着地球转,地球绕着太阳转

在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材质是什么东西,怎么运用

然后给太阳和月亮添加材质,使他们换色

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值