Unity小项目__小球吃零食

 

// Player脚本文件源代码

public class Player : MonoBehaviour
{
    public Rigidbody rd; // 定义了一个刚体组件
    public int score = 0; // 定义了一个计分器
    public Text scoreText; // 定义了一个文本组件
    public GameObject winText; // 定义了一个游戏物体用于检验游戏结束

    // Start is called before the first frame update
    void Start()
    {
        // Debug.Log("游戏开始了!");
        rd = GetComponent<Rigidbody>(); //实现刚体组件
        // rd.AddForce(Vector3.right); // 施加一次持续1N向右的力,由于阻力存在效果不明显
        
        // 监听键盘并控制小球运动
    }

    // Update is called once per frame
    void Update()
    {
        // Debug.Log("游戏正在进行中……");
        // (x, y, z) (1, 0, 0)
        // Vector3.right left forward back
        // rd.AddForce(Vector3.right); // 施加一个持续1N向右的力

        // (2, 0, 0)
        // rd.AddForce(new Vector3(10, 0, 0));
        float h = Input.GetAxis("Horizontal"); // 左右-1 1
        float v = Input.GetAxis("Vertical"); // 前后
        // Debug.Log(h); // 0
        rd.AddForce(new Vector3(h, 0, v) * 2); // 移速*2
    }


    // private void OnCollisionEnter(Collision collision) // 发生碰撞时执行的代码
    //{
        // Debug.Log("发生碰撞了");

        // 判断并销毁食物
        // 当物体的触发器执行后,碰撞器对该物体就失效了,因为触发器不会发生碰撞。
        //if (collision.collider.tag == "Food") // 通过碰撞器碰撞标签的检测
        //if (collision.gameObject.tag == "Food") // 通过碰撞器碰撞标签的检测
        //{
        //    Destroy(collision.gameObject); // 销毁游戏对象
        //}
    // }

    // 碰撞检测
    //private void OnCollisionStay(Collision collision) // 发生碰撞接触时执行的代码
    //{
    //    Debug.Log("发生接触中……");
    //}
    //private void OnCollisionExit(Collision collision) // 发生碰撞离开后执行的代码
    //{
    //    Debug.Log("发生碰撞后离开");
    //}

    // 触发检测(可穿过物体,不会减速)
    private void OnTriggerEnter(Collider other) // 与触发器发生碰撞时执行的代码
    {
        // Debug.Log("OnTriggerEnter" + other.tag);

        if (other.tag == "Food") // 通过碰撞器碰撞标签的检测
        {
            Destroy(other.gameObject); // 销毁游戏对象

            // 自增运算符++
            score++;
            scoreText.text = "分数:" + score; // 引用text属性,将"分数:" + score传递给scoreText.text

            if (score == 8) // 当score等于8时
            {
                winText.SetActive(true); // 设置激活winText游戏物体
            }
        }
    }
    //private void OnTriggerStay(Collider other) // 与触发器接触时执行的代码
    //{
    //    Debug.Log("OnTriggerStay" + other.tag);
    //}
    //private void OnTriggerExit(Collider other) // 离开触发器发生后执行的代码
    //{
    //    Debug.Log("OnTriggerExit" + other.tag);
    //}
}

 // Food

public class Food : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(Vector3.up); // 一度一度的变换角度,60帧/s
    }
}

// FollowTarget

public class FollowTarget : MonoBehaviour
{
    public Transform playerTransform; // 声明一个Transfrom类对象
    private Vector3 offset; // 声明一个结构体对象
    // Start is called before the first frame update
    void Start()
    {
        offset = transform.position - playerTransform.position; // 相机和玩家的三维坐标偏移量 = 相机的三维坐标 - 玩家的三维坐标
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = playerTransform.position + offset; // (改变后)相机改变的三维坐标 = (改变后)玩家对象的三维坐标量 + 初始偏移量;
    }
}

// 界面

 // 打包运行

猜你喜欢

转载自blog.csdn.net/qq_57233919/article/details/132303206