3D游戏(3)——空间与运动

1、简答并用程序验证【建议做】

游戏对象运动的本质是什么?

游戏对象的运动过程本质上就是游戏对象的空间位置(Position)、旋转角度(Rotation)、大小(Scale)三个属性随着时间在做某种特定的变化。

请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)

方法1:

复合运动,先写一个向右运动的脚本,再写一个向下运动的脚本,最后将两个脚本同时挂载到同一个物体上。

向右运动:

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

public class MoveRight : MonoBehaviour
{

    public int speed = 5;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.position += speed * Vector3.right * Time.deltaTime;
    }
}

向下运动:

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

public class MoveDown : MonoBehaviour
{

    public float speed = 2;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.position += speed * Vector3.down * Time.deltaTime;
        speed += 0.1f;
    }
}

方法2:

直接运用Vector3来改变position,将两个脚本合并为一个。

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

public class Move : MonoBehaviour
{

    public int speedx = 5;
    public float speedy = 2;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position += new Vector3(speedx * Time.deltaTime, -1 * speedy * Time.deltaTime, 0);
        speedy += 0.1f;
    }
}

方法3:

使用transform.Translate,将方法2中得出来变化向量传进Translate函数里面。

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

public class Move : MonoBehaviour
{

    public int speedx = 5;
    public float speedy = 2;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(new Vector3(speedx * Time.deltaTime, -1 * speedy * Time.deltaTime, 0));
        speedy += 0.1f;
    }
}

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

首先到红动网下载太阳系的贴图素材。把太阳系各行星摆放好。

在这里插入图片描述

参照练习03-09,首先完成一个在同一法平面上的太阳系。

在这里插入图片描述

要想各行星不在一个法平面上,可以引入一个随机数,随机生成一个法向量,这样就避免了为每一个行星各自设计一个脚本。不过,由于地球还有一个卫星(月球),需要专门设计脚本,因此,总的来说,只需写两个脚本即可。

地球外的行星环绕脚本:

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

public class Round : MonoBehaviour
{
    public Transform center;
    public int y,z,speed;
    // Start is called before the first frame update
    void Start()
    {
        y = Random.Range(-10, 10);
        z = Random.Range(-10, 10);
        speed = Random.Range(10, 100);
    }

    // Update is called once per frame
    void Update()
    {
        transform.RotateAround(center.transform.position, new Vector3(0, y, z), speed * Time.deltaTime);
    }
}

地球的环绕脚本:

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

public class RoundOfEarth : MonoBehaviour
{
    public Transform sun;
    public Transform moon;
    public int y,z,speed;
    // Start is called before the first frame update
    void Start()
    {
        y = Random.Range(-10, 10);
        z = Random.Range(-10, 10);
        speed = Random.Range(10, 100);
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.RotateAround(sun.position, new Vector3(0, y, z), speed * Time.deltaTime);
        moon.transform.RotateAround(this.transform.position, new Vector3(0, y, z), 20 * speed * Time.deltaTime);
    }
}

最终把各自的脚本挂载到每一颗行星上面。

在这里插入图片描述

2、编

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值