• 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. }

  • 相关阅读:
    win10设置透明任务栏
    7000+字图文并茂解带你深入理解java锁升级的每个细节
    手机java游戏下载网站
    论文阅读【Oscar: Object-Semantics Aligned Pre-training for Vision-Language Tasks】
    nginx网站服务概述
    测试代码1
    linux系统服务管理systemctl 和systemd
    Git 分支管理规范
    【FFH】啃论文俱乐部---世界上最快的C语言JSON库
    【k8s】1、基础概念和架构及组件
  • 原文地址:https://blog.csdn.net/leoysq/article/details/133787463