Unity3D写雷电游戏(三)

现在开始真正的游戏元素的编写了。

第一步,让飞机动起来。

首先是飞机的前进,通常2D中的做就是背景的循环滚动。

在3D中我们可以让摄像机移动,背景我们可以做超一个大地形。。在地形上摆一些固定的东西。

    // Update is called once per frame
void Update () {

TurnLeft = false;
TurnRight = false;

if (Input.GetKey(KeyCode.W))
{
Vector3 screenPos = Camera.mainCamera.WorldToScreenPoint(this.transform.position);
//print(screenPos.y);
if (Screen.height > screenPos.y)
this.transform.Translate(Vector3.forward * Time.deltaTime * m_nMoveSpeed);
}
if (Input.GetKey(KeyCode.S))
{
Vector3 screenPos = Camera.mainCamera.WorldToScreenPoint(this.transform.position);
if (0 < screenPos.y)
this.transform.Translate(Vector3.forward * Time.deltaTime * -m_nMoveSpeed);
}

if (Input.GetKey(KeyCode.A))
{
Vector3 screenPos = Camera.mainCamera.WorldToScreenPoint(this.transform.position);
if (0 < screenPos.x)
this.transform.Translate(Vector3.left * Time.deltaTime * m_nMoveSpeed);
//向左转
if (CurRotation < RotateLimit)
{
print(CurRotation);
CurRotation += RotateSpeed;
}
TurnLeft = true;
}


if (Input.GetKey(KeyCode.D))
{
Vector3 screenPos = Camera.mainCamera.WorldToScreenPoint(this.transform.position);
if (Screen.width > screenPos.x)
this.transform.Translate(Vector3.left * Time.deltaTime * -m_nMoveSpeed);
//向右转
if (CurRotation > -RotateLimit)
CurRotation -= RotateSpeed;
TurnRight = true;
}

//回归
if (!TurnLeft && !TurnRight)
{
if (CurRotation > 0.0)
CurRotation -=RotateSpeed;
else if (CurRotation < 0)
CurRotation +=RotateSpeed;
}




Quaternion rot = Quaternion.AngleAxis(CurRotation, new Vector3(0, 0, 1));
m_Plane.rotation = rot;

//让相机和飞机一起以一定的速度前移
this.transform.Translate(Vector3.forward * Time.deltaTime * m_nMoveSpeed);
Camera.mainCamera.transform.Translate(Vector3.up * Time.deltaTime * m_nMoveSpeed);

}

飞机的主要控制代码。。不知为什么,我的两个角度限制没有效果。。郁闷。。有空还看一下。。



原文地址:https://www.cnblogs.com/gameprogram/p/2285414.html