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. 力扣Leetcode 572. 另一个树的子树

    另一个树的子树 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 ...

  2. Framework项目持续集成(jenkins)及集合SonarQube

    目录 Framework项目持续集成及集合SonarQube - 前言 CI/CD是什么 -- jenkins 什么是sonarqube?什么是CodeReview? 什么是sonarqube? 什么 ...

  3. 招新裁老,两面派互联网大厂,培训三个月,就拿15K,凭什么?

    看到一位朋友在发帖子求问:亲身经历,(如有谎言我名字倒过来写)一个大学同学18年毕业的.在兰州一个二本学的兽医农牧,毕业难找工作,去深圳一个机构培训了三个月吧,然后就去做大数据 算法了,然后又去做ja ...

  4. gdb我在我本机上p不了,在别人机子上可以

    gdb我在我本机上p不了,在别人机子上可以,不知道什么 (gdb) p EventFlow->devicetypeThere is no member or method named devic ...

  5. Labview学习之路(一)程序框图中的修饰

    很多小伙伴知道在前面板有很多修饰符,比如上凸框,加粗下凹框等等,但是其实在程序框图中也是有修饰符的,他的位置比较隐蔽,并且修饰符很少,所以很多人基本没有用过.现在就给大家介绍一些这些程序框图种的修饰. ...

  6. jq cdn地址

    百度CDN支持版本2.0.3, 2.0.2, 2.0.1, 2.0.0,1.11.1, 1.10.2, 1.10.1, 1.10.0, 1.9.1, 1.9.0, 1.8.3, 1.8.2, 1.8. ...

  7. fragment没有getWindowManager 关于fragment下的报错解决方法

    其实很简单:只需要在getWindowManager().getDefaultDisplay().getMetrics(metric) 前面加上getactivity()即可.

  8. Linux服务器关联Git,通过执行更新脚本实现代码同步

    1.在Linux服务器安装Git yum install git -y   tips: 卸载Git :  yum remove git   2.在Linux生成ssh key   1)创建用户 git ...

  9. 前端code导入excel

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. IE9 报错 script1004缺少“;”

    在IE9中不支持 let关键字,修改为var就可以了 作者:彼岸舞 时间:2020\07\31 内容关于:工作中用到的小技术 本文来源于网络,只做技术分享,一概不负任何责任