3D游戏编程第三次作业

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

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

游戏对象运动的本质是游戏对象Position、Rotate、Scale属性数值的变化。

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

  • 使用Vector3
 public int xSpeed = 1;  //单位时间x方向的位移量
public int ySpeed = 1; //单位时间y方向的位移量
public int T = 1; //时间
void Update()
{
transform.position += Vector3.right * Time.deltaTime * xSpeed;
transform.position += Vector3.down * Time.deltaTime * ySpeed * Time.deltaTime * T;
T++;
}
  • 使用Transform.Translate
 public int xSpeed = 1;  //单位时间x方向的位移量
public int ySpeed = 1; //单位时间y方向的位移量
public int T = 1; //时间
void Update()
{
transform.Translate(Vector3.right * Time.deltaTime * xSpeed + Vector3.down * Time.deltaTime * ySpeed * Time.deltaTime * T);
T++;
}
  • 直接修改transform
 public int speed = 2;

 void Update()
{
transform.position += new Vector3(Time.deltaTime * speed, -Time.deltaTime * speed * (2 * transform.position.x + Time.deltaTime * speed), 0);
}

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

仿照如上课堂的练习,我们可以将太阳系的八大行星都列出来,为他们的position赋值,并且为了满足围绕太阳转速不同以及不在同一法平面的要求,我们额外增加一个向量来表示他们的转动方向,并且在执行RotateAround时传入不同的转动周期。代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class SolarSystem : MonoBehaviour
{
public Transform Sun, Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune;
Vector3 vectMercury, vectVenus, vectEarth, vectMoon, vectMars, vectJupiter, vectSaturn, vectUranus, vectNeptune;
// Start is called before the first frame update
void Start()
{
// 初始化位置
Sun.position = Vector3.zero;
Mercury.position = new Vector3(8, 0, 0);
Venus.position = new Vector3(10, 0, 0);
Earth.position = new Vector3(15, 0, 0);
Moon.position = new Vector3(18, 0, 0);
Mars.position = new Vector3(24, 0, 0);
Jupiter.position = new Vector3(34, 0, 0);
Saturn.position = new Vector3(44, 0, 0);
Uranus.position = new Vector3(51, 0, 0);
Neptune.position = new Vector3(55, 0, 0);
// Pluto.position = new Vector3(58, 0, 0); // 初始化方向向量
vectMercury = new Vector3(3, 11, 0);
vectVenus = new Vector3(2, 3, 0);
vectEarth = new Vector3(0, 1, 0);
vectMoon = Vector3.up;
vectMars = new Vector3(1, 5, 0);
vectJupiter = new Vector3(1, 5, 0);
vectSaturn = new Vector3(1, 9, 0);
vectUranus = new Vector3(2, 7, 0);
vectNeptune = new Vector3(1, 5, 0);
// vectPluto = new Vector3(1, 3, 0);
} // Update is called once per frame
void Update()
{
// 以向量向太阳做公转
Mercury.RotateAround(Sun.position, vectMercury, 10 * Time.deltaTime);
Venus.RotateAround(Sun.position, vectVenus, 30 * Time.deltaTime);
Earth.RotateAround(Sun.position, vectEarth, 20 * Time.deltaTime);
Moon.RotateAround(Earth.position, Vector3.up, 359 * Time.deltaTime);
Mars.RotateAround(Sun.position, vectMars, 60 * Time.deltaTime);
Jupiter.RotateAround(Sun.position, vectJupiter, 5 * Time.deltaTime);
Saturn.RotateAround(Sun.position, vectSaturn, 6 * Time.deltaTime);
Uranus.RotateAround(Sun.position, vectUranus, 35 * Time.deltaTime);
Neptune.RotateAround(Sun.position, vectNeptune, 10 * Time.deltaTime);
// Pluto.RotateAround(Sun.position, vectPluto, 20 * Time.deltaTime); // 自转速度
Sun.Rotate(Vector3.up * 10 * Time.deltaTime);
Mercury.Rotate(Vector3.up * 600 * Time.deltaTime);
Venus.Rotate(Vector3.up * 400 * Time.deltaTime);
Earth.Rotate(Vector3.up * 360 * Time.deltaTime);
Moon.Rotate(Vector3.up * 1000 * Time.deltaTime);
Mars.Rotate(Vector3.up * 300 * Time.deltaTime);
Jupiter.Rotate(Vector3.up * 300 * Time.deltaTime);
Saturn.Rotate(Vector3.up * 200 * Time.deltaTime);
Uranus.Rotate(Vector3.up * 400 * Time.deltaTime);
Neptune.Rotate(Vector3.up * 500 * Time.deltaTime);
// Pluto.Rotate(Vector3.up * 400 * Time.deltaTime);
}
}

在制作脚本之后,我们需要为脚本制作prefabs:

并且根据不同星球的大小制作不同的属性:

最终程序运行效果如下:

特别注意的是,在制作prefabs之后每个星球的颜色基本是看不见的,这是因为unity中默认使用的是平行光源,我们可以在太阳处增加一个range极大的点光源,这样就可以基本看出一个太阳系的特点:

编程实践

思考题【选做】

  • 使用向量与变换,实现并扩展 Tranform 提供的方法,如 Rotate、RotateAround 等

其中position表示物体的位置,rotation表示物体的角度

Rotate:

void Rotate(Transform t,Vector3 axis,float angle){
var rot=Quaternion.AngleAxis(angle,axis);
t.rotation*=rot;
}

RotateAround:

void RotateAround(Transform t,Vector3 center,Vector axis,float angle){
var rot=Quaternion.AngleAxis(angle,axis);
t.position=(center+(t.position-center)*rot);
t.rotation=t.rotation*rot;
}

unity 3d 三、空间与运动的更多相关文章

  1. Unity 3d 实现物体跟随摄像机视野运动

    https://blog.csdn.net/qq_31411825/article/details/61623857 Unity 3d 实现物体跟随摄像机视野运动Created by miccall ...

  2. C#程序员整理的Unity 3D笔记(十):Unity3D的位移、旋转的3D数学模型

    遇到一个想做的功能,但是实现不了,核心原因是因为对U3D的3D数学概念没有灵活吃透.故再次系统学习之—第三次学习3D数学. 本次,希望实现的功能很简单: 如在小地图中,希望可以动态画出Player当前 ...

  3. Unity 3D Framework Designing(7)——IoC工厂理念先行

    一谈到 『IoC』,有经验的程序员马上会联想到控制反转,将创建对象的责任反转给工厂.IoC是依赖注入 『DI』 的核心,大名鼎鼎的Spring框架就是一个非常卓越的的控制反转.依赖注入框架.遗憾的是, ...

  4. Unity 3D中的阴影设置

    在Unity 3D中,经常需要用到光照阴影,即Directional Light的Shadow,Shadow分为Hard Shadow和Soft Shadow.区别是Soft Shadow的阴影边缘比 ...

  5. unity 3D 学习笔记

    1.父对象的初始位置设,即刚开始的空对象的根节点位置应当设置成(0,0,0) 这样设置可以避免以后出现奇怪的坐标. GameObject实际上就是一些组件的容器. unity 使用公用变量原因是,在U ...

  6. 使用 Unity 3D 开发游戏的架构设计难点

    Unity 3D 引擎对于开发者来说,入手非常快,因为它采用的是 C# 作为开发语言,这也大大降低了开发者的门槛.但凡只要懂一门编程语言的人都能使用 Unity 3D 引擎开发,另外 Unity 3D ...

  7. 【转】UNITY中相机空间,投影空间的正向问题

    原文链接1:https://www.cnblogs.com/wantnon/p/4570188.html 原文链接2:https://www.cnblogs.com/hefee/p/3820610.h ...

  8. Unity 3D 游戏上线之后的流水总结

    原地址:http://tieba.baidu.com/p/2817057297?pn=1 首先.unity 灯光烘焙 :Unity 3D FBX模型导入.选项Model 不导入资源球.Rig 不导入骨 ...

  9. 再议Unity 3D

    一年前,偶发冲动,翻译了<[译] Unity3D游戏和facebook绑定(1:简介)>系列文章. 现在看有2个明显的好处, 一:给这个不温不火的博客带了top 3的人气: 二:我个人由此 ...

随机推荐

  1. Linux下关闭和开启IPv6的方法

    确认IPV6是否开启 在Linux下确认IPv6是否已经被启用,可以从三个方面确定. 1.使用ifconfig查看自己的IP地址是否含有IPv6地址. eth0 Link encap:Ethernet ...

  2. sourse insight总是代码一写长了,就自动换行

    sourse insight总是代码一写长了,就自动换行. Document Options里面Word Wrap选项不勾选.

  3. Java开发环境搭建的准备工作

    Java开发环境搭建的准备工作 网络配置(修改hosts) 什么时候需要 比如我们在安装homeBrew的时候会遇到 curl: (7) Failed to connect to raw.github ...

  4. Spring Security如何优雅的增加OAuth2协议授权模式

    一.什么是OAuth2协议? OAuth 2.0 是一个关于授权的开放的网络协议,是目前最流行的授权机制. 数据的所有者告诉系统,同意授权第三方应用进入系统,获取这些数据.系统从而产生一个短期的进入令 ...

  5. Mysql宽字节注入 ---学习笔记

    转自:https://blog.csdn.net/niexinming/article/details/49109683 先补充一点背景:大 家都知道PHP在开启magic_quotes_gpc或者使 ...

  6. Mysql如何将某个字段的值,在原有的基础上+1?

    Eg: 电商项目中,需要统计某件商品的购买数量问题,这时产品提了一个bug,告诉你我需要你做一个购买数量统计?你会怎么做呢? 这里我只说我自己的思路,首先是浏览加购物车,创建订单并支付,mq消息消费后 ...

  7. [BUUOJ记录] [极客大挑战 2019]RCE ME

    前面考察取反或者异或绕过,后面读Flag那里我用脏方法过了,没看出来考察啥 进入题目给出源码: <?php error_reporting(0); if(isset($_GET['code']) ...

  8. Java8 Functional(函数式接口)

    Functional 函数式(Functional)接口 只包含一个抽象方法的接口,称为函数式接口. 你可以通过 Lambda 表达式来创建该接口的对象.(若 Lambda 表达式抛出一个受检异常(即 ...

  9. docker push出现denied: requested access to the resource is denied

    今天想再 把本地的docker image 镜像push 到: https://hub.docker.com/ Step1: login : https://hub.docker.com/ [root ...

  10. 故事:坐在我隔壁的小王问我什么是HyperLogLog

    1 最近坐我隔壁的小王同志,心情真是糟透了.不但工作不顺心,被老板狠狠的批了一顿,连女朋友也跟别人跑了(Y 的让你天天在我面前秀). 真是不可谓不惨,我都快要同情他了. 看着他萎靡又迷离的眼神,我实在 ...