TankTutorial
声明:所有图片均来源于Unity3d官网案例Tank Turtorial中下载的pdf教案
Tank Tutorial-传送门
1.Perspective(透视)和Orthographic(正交)视图的区别?
透视模式的Camera中,画面呈现近大远小效果(左图)
正交模式的Camera中,画面呈现场景中各物体到摄像机的距离相等效果(右图)
2.Camera Scrpits中的Zoom问题?
/*
* CameraControl Script - FindRequiredSize()
* https://unity3d.com/learn/tutorials/projects/tanks-tutorial/camera-control?playlist=20081
*/
private float FindRequiredSize()
{
// Find the position the camera rig is moving towards in its local spcae.
Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition);
// Start the camera's size calculation at zero.
float size = 0f;
// Go through all the targets...
for (int i = 0; i < m_Targets.Length; i++) {
// ... and if they aren't active continue on to the next target.
if (!m_Targets [i].gameObject.activeSelf)
continue;
// Otherwisee, find the position of the target in the camera's local space.
Vector3 targetLocalPos = transform.InverseTransformPoint(m_Targets[i].position);
// Find the position of the target from the desired position of the camera's local spcae.
Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos;
// Choose the largest out of the current size and the distance of the tank 'up' or 'down' from the camera.
size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.y));
// Choose the largest out of the current size and the calculated size based on the tank being to the left or right of the camera.
size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.x)/m_Camera.aspect);
}
// Add the edge buffer to the size.
size += m_ScreenEdgeBuffer;
// Make sure the camera's size isn't below the minimum.
size = Mathf.Max(size, m_MinSize);
return size;
}
正交Camera的Zoom是通过调整Size来实现的。
调整Camera的SIZE值使得两辆Tank都能出现在画面中。方法如下:
首先解释ASPECT是怎么得到的:
例子:
1080p表示屏幕显示是由1920 * 1080个像素组成的;
1920 * 1080 的aspect ratio为16:9;
aspect = 16/9 = 1.778;
Camera物体本身具有aspect属性
为了保证Camera中能够完全显示所有玩家的Tank,Zoom函数要计算出最适合的Size.。
首先,通过移动Camera的Y轴(注意是绿绿的Y轴),直到所有Tank都在Camera中出现,则此时Camera的偏移量可被视作SIZE.
其次,Camera所在的位置和最远处Tank的距离向量在水平方向上的距离可被视为SIZE*ASPECT.
最后,在两个SIZE中择优(大),取得的SIZE能够保证所有Tank都处在Camera的画面中,但由于视野范围太小,看起来不舒服,因此需要添加额外的SIZE来扩大Camera的视野,同时设立最小SIZE能够保证当两个Tank靠得太近时,视野不至于太小。
3.通过Physics.OverLapSphere函数可以收集指定范围内所有物体的信息.
4.在进行GameObject的处理时要时刻注意:
最好是对空物体添加组件而不是对已经制作好的Model添加组件;
在动画制作中称为层次动画:针对群组节点制作动画而不是几何体。
5.当一个GameObject包含太多的Scripts时:
可以建立一个不继承任何类的脚本去管理该GameObject.例如TankManager。
6.Shooting Script中对if语句的顺序迷茫时:
可以根据玩家操作顺序来决定,先点击按钮-保持按钮-松开按钮.。
7.游戏整体逻辑非常重要,要是设计不得当会导致编码时bug频出,思维混乱。因此,事先构图是很有必要的,需要养成习惯,多加练习。
8.下一个回顾练习 Survival Shooter! 这会是一个漫长的旅途,忍耐忍耐,一定能够找到游戏的美妙之处!