大家好,我是Zander,我们接着来开发Rts3D即时战略游戏开发。在 游戏中我们要建筑也能生产无人机,这一章我们就来实现一下具体操作。
首先在Actions文件夹下创建CreateUnitAction脚本并编辑,如下:
using UnityEngine;
using System.Collections;
public class CreateUnitAction : ActionBehavior {
public GameObject Prefab; //生产单位
public float Cost = 0; //生产单位所需金币
private PlayerSetupDefinition player;//所属玩家信息
// Use this for initialization
void Start () {
player = GetComponent<Player> ().Info;
}
public override System.Action GetClickAction ()
{
return delegate() {
if (player.Credits < Cost) {
Debug.Log ("Cannot Create, It costs " + Cost);
return;
}
var go = (GameObject)GameObject.Instantiate (
Prefab,
transform.position,
Quaternion.identity);
go.AddComponent<Player> ().Info = player;
go.AddComponent<RightClickNavigation> ();
go.AddComponent<ActionSelect> ();
player.Credits -= Cost;
};
}
}
切换到FindBuildingSite脚本,在Update函数下,添加以下代码:
void Update () {
var tempTarget = RtsManager.Current.ScreenPointToMapPosition (Input.mousePosition);
if (tempTarget.HasValue == false)
return;
transform.position = tempTarget.Value;
if (Vector3.Distance (transform.position, Source.position) > MaxBuildDistance) {
rend.material.color = Red;
return;
}
if (RtsManager.Current.IsGameObjectSafeToPlace (gameObject)) {
rend.material.color = Green;
if (Input.GetMouseButtonDown (0)) {
var go = GameObject.Instantiate (BuildingPrefab);
go.AddComponent<ActionSelect> (); //增加选择动作
go.transform.position = transform.position;
Info.Credits -= Cost;
go.AddComponent<Player> ().Info = Info;
Destroy (this.gameObject);
}
} else {
rend.material.color = Red;
}
}
然后返回的Unity中,在Prefabs文件夹下找到Command Base预设,为其添加CreateBuildingAction组件,并为其赋值,如图所示
现在就可以自动生产无人机了。
好了,这一章就写到这,接下来的几张我将为大家介绍一下AI。欢迎大家加入QQ群:280993838 或者关注我的公众号: