unity 3d 三、空间与运动
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 三、空间与运动的更多相关文章
- Unity 3d 实现物体跟随摄像机视野运动
https://blog.csdn.net/qq_31411825/article/details/61623857 Unity 3d 实现物体跟随摄像机视野运动Created by miccall ...
- C#程序员整理的Unity 3D笔记(十):Unity3D的位移、旋转的3D数学模型
遇到一个想做的功能,但是实现不了,核心原因是因为对U3D的3D数学概念没有灵活吃透.故再次系统学习之—第三次学习3D数学. 本次,希望实现的功能很简单: 如在小地图中,希望可以动态画出Player当前 ...
- Unity 3D Framework Designing(7)——IoC工厂理念先行
一谈到 『IoC』,有经验的程序员马上会联想到控制反转,将创建对象的责任反转给工厂.IoC是依赖注入 『DI』 的核心,大名鼎鼎的Spring框架就是一个非常卓越的的控制反转.依赖注入框架.遗憾的是, ...
- Unity 3D中的阴影设置
在Unity 3D中,经常需要用到光照阴影,即Directional Light的Shadow,Shadow分为Hard Shadow和Soft Shadow.区别是Soft Shadow的阴影边缘比 ...
- unity 3D 学习笔记
1.父对象的初始位置设,即刚开始的空对象的根节点位置应当设置成(0,0,0) 这样设置可以避免以后出现奇怪的坐标. GameObject实际上就是一些组件的容器. unity 使用公用变量原因是,在U ...
- 使用 Unity 3D 开发游戏的架构设计难点
Unity 3D 引擎对于开发者来说,入手非常快,因为它采用的是 C# 作为开发语言,这也大大降低了开发者的门槛.但凡只要懂一门编程语言的人都能使用 Unity 3D 引擎开发,另外 Unity 3D ...
- 【转】UNITY中相机空间,投影空间的正向问题
原文链接1:https://www.cnblogs.com/wantnon/p/4570188.html 原文链接2:https://www.cnblogs.com/hefee/p/3820610.h ...
- Unity 3D 游戏上线之后的流水总结
原地址:http://tieba.baidu.com/p/2817057297?pn=1 首先.unity 灯光烘焙 :Unity 3D FBX模型导入.选项Model 不导入资源球.Rig 不导入骨 ...
- 再议Unity 3D
一年前,偶发冲动,翻译了<[译] Unity3D游戏和facebook绑定(1:简介)>系列文章. 现在看有2个明显的好处, 一:给这个不温不火的博客带了top 3的人气: 二:我个人由此 ...
随机推荐
- vue中页面卡顿,使用懒加载
为给客户更好的客户体验,首屏组件加载速度更快一些,解决白屏问题. 懒加载简单来说就是延迟加载或按需加载,即在需要的时候的时候进行加载. 常用的懒加载方式有两种:即使用vue异步组件 和 ES中的imp ...
- Python 逆向抓取 APP 数据
今天继续给大伙分享一下 Python 爬虫的教程,这次主要涉及到的是关于某 APP 的逆向分析并抓取数据,关于 APP 的反爬会麻烦一些,比如 Android 端的代码写完一般会进行打包并混淆加密加固 ...
- OC基础--数据类型与表达式
前言 做iOS开发有3年了,从当初的小白到现在,断断续续看过很多资料,之前也写过一些博文来记录,但是感觉知识点都比较凌乱.所以最近准备抽时间把iOS开发的相关知识进行一个梳理,主要分为OC基础.UI控 ...
- 【目标检测】SSD+Tensorflow 300&512 配置详解
SSD_300_vgg和SSD_512_vgg weights下载链接[需要科学上网~]: Model Training data Testing data mAP FPS SSD-300 VGG-b ...
- Weights and Measures (贪心+dp)
I know, up on top you are seeing great sights, But down at the bottom, we, too, should have rights. ...
- 执行Python程序出现“SyntaxError: Non-UTF-8 code starting with '\xb6'...”错误怎么办?
如果文件中有中文,直接执行python xx.py会出现以下错误: SyntaxError: Non-UTF- code starting with '\xb6' in file XX.py on l ...
- fabric1.4 网络操作
建立第一个网络 进入对应目录 $ cd fabric-samples/first-network 在first-network目录下有两个自动化脚本byfn.sh和eyfn.sh, 这两个脚本的启动顺 ...
- 你必须要知道的babel二三事
1. 什么是babel 本文基于的babel版本是7.11.6,本文所有示例github Babel is a toolchain that is mainly used to convert ECM ...
- A Funny Game(POJ 2484)
原题如下: A Funny Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7108 Accepted: 446 ...
- App测试理论简介
一.App测试常见关注点 1.App的功能测试 功能测试都是我们首要测试的,只有功能实现了才算符合上线发布的最低标准.我们需要检测产品功能是否已实现.产品功能是否符合设计要求.产品功能是否有重复.产品 ...