接物体的工具Demon_接金币(三个掉落物品预设体,一接物体的工具)-LMLPHP

using UnityEngine;
using System.Collections; public class Tool : MonoBehaviour { float hor; Vector3 moveDir; public float moveSpeed = 3f; int score = ; void Update()
{
hor = Input.GetAxis ("Horizontal");
//获取移动方向向量
moveDir = hor * Vector3.right;
//移动
transform.position += moveDir * Time.deltaTime * moveSpeed;
} void OnTriggerEnter(Collider other)
{
if (other.tag == "Gold") {
Destroy (other.gameObject);
Debug.Log (++score);
}
} }

创建掉落体的三个预设体

Demon_接金币(三个掉落物品预设体,一接物体的工具)-LMLPHP

using UnityEngine;
using System.Collections; public class GoldCreater : MonoBehaviour { //金币预设体
public GameObject goldPrefab;
//生成金币的时间间隔
public float interval = 1f;
//计时器
private float timer;
//索引号
private int index; void Update()
{
//计时器计时
timer += Time.deltaTime;
//计时完成
if (timer >= interval) {
///TODO:生成金币
GoldInit();
//计时器归零
timer = ;
}
} /// <summary>
/// 生成金币
/// </summary>
void GoldInit()
{
//子对象索引号(随机)
index = Random.Range (, );
//生成位置
Vector3 initPos = transform.GetChild (index).position;
//生成金币
Instantiate (goldPrefab, initPos, Quaternion.identity);
} }
04-17 14:20