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. py_创建文件以及写入读取数据+异常处理

    import readline import math import json ''' A: 第一行 第二行 第三行 ''' #从文件读取数据 with open("D:\A.txt&quo ...

  2. 基于Rust-vmm实现Kubernetes运行时

    随着容器及K8s的广泛使用,越来越多的容器安全与隔离问题被暴露出来,如:容器逃逸.水平攻击.DDos攻击等严重威胁了办公和生产环境的安全与稳定,影响了业务的正常运行.安全容器技术孕育而生,产生了kat ...

  3. js中call,apply和bind

    1,首先先做一个定义:每个函数都包含两个非继承的方法:apply()和call(),apply和call这两个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体内this对象的值,两者唯一的 ...

  4. C# Chart各个属性详细解析、应用

    Chart笔记 前台页面代码: <form id="form1" runat="server"> <div> <asp:Chart ...

  5. Infinite Inversions(树状数组+离散化)

    思路及代码参考:https://blog.csdn.net/u014800748/article/details/45420085 There is an infinite sequence cons ...

  6. LaTeX分分钟上手【转】

    原文地址:<LaTeX新人教程,30分钟从完全陌生到基本入门> 需要说明的几点: 1.文中说用XeTex,但是我的总是失败(出现!undefined control sequence.), ...

  7. Agumaster点个按钮后台刷新前台一步步出状态

    --2020年5月4日--

  8. 求支付表中按id累积和最接近100的那条记录

    此例源自美团的一道SQL面试题 支付表结构: create table hy_payment( id number(4,0) primary key, pay number(3,0) not null ...

  9. 营销经验总结:如何才能提升h5游戏代入感?

    HTML5游戏拥有即点即玩,无需下载,并具备传播性广的特点,这就使得商家看到了无限商机,如何让产品更加深入人心,是游戏推广最为重要的环节.优秀的代入感才是游戏产品宣传的关键,那么有哪些要素的支撑才能确 ...

  10. 吴恩达《深度学习》-课后测验-第一门课 (Neural Networks and Deep Learning)-Week 4 - Key concepts on Deep Neural Networks(第四周 测验 – 深层神经网络)

    Week 4 Quiz - Key concepts on Deep Neural Networks(第四周 测验 – 深层神经网络) \1. What is the "cache" ...