这个示例使用了官方教程的素材,自建脚本之后实现与原游戏的不同感觉。
1.这个坦克大战主要是实现了几组敌对坦克的互相混战(通过设置不同的层级来识别)。AI坦克有搜寻敌对坦克,自动寻路自动开火功能。
由本方的炮弹击中敌对的坦克会对对方产生力的效果并执行伤害计算。坦克死亡的时候会生成死亡坦克对象并播放爆炸效果;
1.玩家视角控制脚本
using UnityEngine;
using System.Collections;
public class FYLcamera : MonoBehaviour {
public Transform target;//被观测物体
public Transform childCamera;//摄像机
public float rotateSpeed;//旋转速度
public float scaleSpeed;//缩放速度
public float minDistance;//最近缩放位置
public float maxDistance;//最远缩放位置
private float currentScale;//现有的缩放大小
private Vector3 currentRotation;//现有的旋转角
private Vector3 lastMousePosition;//记录鼠标上次的位置
// Use this for initialization
void Start () {
currentScale = childCamera.position.z;//初始化缩放大小
currentRotation = transform.eulerAngles;//初始化欧拉角
lastMousePosition = Input.mousePosition;
}
// Update is called once per frame
void LateUpdate () {
if (target == null)
return;
Vector3 mouseDelta = Input.mousePosition - lastMousePosition;//计算鼠标位移
lastMousePosition = Input.mousePosition;
if (Input.GetMouseButton (1)) {
//如果使用鼠标右键
currentRotation.x += mouseDelta.y * rotateSpeed * Time.deltaTime;
currentRotation.y += mouseDelta.x * rotateSpeed * Time.deltaTime;
//因为旋转方向和鼠标拖动方向正好相反,所以用以上公式计算旋转量
}
currentScale += -Input.mouseScrollDelta.y * scaleSpeed * Time.deltaTime;//计算缩放值,因为我的摄像机是反的,所以要取负数
currentScale = Mathf.Clamp (currentScale, minDistance, maxDistance);//取闭区间,使得缩放值一直在规定的范围之内
transform.position = target.position;//空物体的坐标跟随被观测物体
transform.eulerAngles = currentRotation;//欧拉角设定为当前的旋转量
childCamera.localPosition = new Vector3 (0, 0, currentScale);//设定摄像机的缩放距离。
}
}
2.玩家行动开火控制脚本
using UnityEngine;
using System.Collections;
public class playercontroller : Unit {
public float moveSpeed;//移动速度
public float rotateSpeed;//旋转速度
public int MaxHealth=200;//最大生命值,用于画血条
private Tankweapon tk;
public int getMaxHealth()
{
return MaxHealth;
}
void Start()
{
tk = GetComponent<Tankweapon> ();
tk.Init(team);
}
void FixedUpdate ()
{
float horizontal = I