unity开发案例RollABall(9)之设置UI积分

首先,我们需要再玩家脚本中定义一个score,用于存储分数。

设置分数初始值为0,每次触发碰撞事件之后,score++:

接着,我们需要创建一个UI界面:

设置好text的位置等等。

 接着,就是监听事件检测了。

监听事件代码如下:

扫描二维码关注公众号,回复: 16561628 查看本文章
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class player_move : MonoBehaviour
{
    public int score = 0;
    public Text ScoreText;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Rigidbody rd = GetComponent<Rigidbody>();
        rd.AddForce(new Vector3(h, 0, v) * 13);



    }

   

    private void OnTriggerEnter(Collider other)
    {
       
            if (other .tag == "food")
            {
                Destroy(other .gameObject);
            score++;
            ScoreText.text = "分数:" + score;
            }
        
    }
}

猜你喜欢

转载自blog.csdn.net/qq_51196701/article/details/123025187