• Unity基础课程之物理引擎8-扔保龄球游戏案例(完)


    三个脚本:

    1.给求添加力

    2.分数管理器

    3.检测是否发生碰撞

    -----------------------------------------------

    脚本源码

    1.给求添加力

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class RoleControl : MonoBehaviour
    5. {
    6. // 1.玩家控制一个保龄球,按下空格键,开始发射
    7. // 拿到物体
    8. GameObject MainRole;
    9. Rigidbody Onerigi;
    10. bool isDown = true;
    11. public float ForceDATA = 100f;
    12. void Start()
    13. {
    14. MainRole = GameObject.Find("MainRole");
    15. }
    16. void Update()
    17. {
    18. if (Input.GetKeyDown(KeyCode.Space)&&isDown)//如果用户按下空格键
    19. {
    20. //开始添加力给MainRole
    21. Onerigi = MainRole.GetComponent();
    22. Onerigi.AddForce(new Vector3(0, 0, -1)* ForceDATA, ForceMode.Impulse);
    23. isDown = false;
    24. }//end if
    25. }//end update
    26. }//end class

    2.分数管理器

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. public class ScoreManager : MonoBehaviour
    6. { // 如果撞倒花瓶就加分
    7. TMP_Text OneWenBen;
    8. GameObject wenben;
    9. public static int currentScore = 0;
    10. private void Start()
    11. {
    12. wenben = GameObject.Find("Text (TMP)");
    13. OneWenBen = wenben.GetComponent();
    14. }
    15. private void LateUpdate()
    16. {
    17. Debug.Log("恭喜你!得分了!你的分数是:" + currentScore);
    18. OneWenBen.text ="Score:"+ currentScore.ToString();
    19. }
    20. }

    3.检测是否发生碰撞

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class CollisionTest : MonoBehaviour
    5. {
    6. // 碰撞检测
    7. //private void OnTriggerEnter(Collider other)
    8. //{
    9. // //发生了碰撞
    10. // ScoreManager.currentScore += 1;//通知分数管理器类加分
    11. // Debug.Log("开始碰撞");
    12. // Debug.Log(other.gameObject.name);
    13. //}
    14. private void OnCollisionEnter(Collision collision)
    15. {
    16. Debug.Log("开始碰撞");
    17. if (collision.collider.gameObject.name!="Plane"&& collision.collider.gameObject.name != "Cube")
    18. {
    19. ScoreManager.currentScore += 1;
    20. //通知分数管理器类加分
    21. }
    22. Debug.Log(collision.collider.gameObject.name);
    23. }
    24. }

  • 相关阅读:
    python+django校园社交高校交友网站2x7r5.
    Codeforces Round #821 (Div. 2)(A~E)
    2022年全球市场单线激光雷达总体规模、主要生产商、主要地区、产品和应用细分研究报告
    C++中的代码重用
    算法模块如何保证依赖数据的同步
    http和https区别,第三方证书如何保证服务器可信
    iloc函数
    我是如何构建自己的笔记系统的?
    vSphere 架构设计方案(下)
    Netty 使用数字证书建立tsl(ssl),检查crl(证书吊销列表)
  • 原文地址:https://blog.csdn.net/leoysq/article/details/133787463