using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public int score = 0;
public float moveSpeed = 10f;
private float hor;
private void Update(){
hor = Input.GetAxis ("Horizontal");
transform.position += Vector3.forward * hor * Time.deltaTime * moveSpeed;
}
private void OnTriggerEnter(Collider other){
if (other.tag == "Coin") {
Destroy (other.gameObject);
print (score++);
}
}
}
using UnityEngine;
using System.Collections;
public class CoinManager : MonoBehaviour {
[Header("金币的预制体")]
public GameObject coinPrefab;
public float coinY=6;
[Header("金币生成的时间间隔")]
public float interval=2f;
//2s
//计时器
private float timer;
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
//计时器记录当前帧时间
if(timer>interval){
//生成金币
GameObject coin=(GameObject)Instantiate (coinPrefab, new Vector3 (0, coinY, Random.Range (-3, 3)), Quaternion.identity);
//定时销毁
Destroy(coin,2);
timer = 0;
}
}
}
网友评论