Unity实战 RTS3D即时战略游戏开发(十一) 单位生产

      大家好,我是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  或者关注我的公众号:

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值