美文网首页
Unity-实现接金币(随机位置)

Unity-实现接金币(随机位置)

作者: ssttIsme | 来源:发表于2022-12-18 10:49 被阅读0次


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;
        }

    }
}

相关文章

网友评论

      本文标题:Unity-实现接金币(随机位置)

      本文链接:https://www.haomeiwen.com/subject/hpmrqdtx.html