许可优化
产品
解决方案
服务支持
关于
软件库
当前位置:服务支持 >  软件文章 >  Unity3d Survival Shooter Tutorial 学习笔记(七)---计分

Unity3d Survival Shooter Tutorial 学习笔记(七)---计分

阅读数 5
点赞 0
article_banner

1.把sense设置为2D模式

2.在HUDCanvas下添加子对象ScoreText

3.设置锚点

4.设置ScoreText位置。

注意,ScoreText位置的位置是相对于锚点而言的!,与锚点距离y方向相差55

5.设置ScoreText参数

6.添加Shadow

7.为ScoreText添加脚本ScoreManager.cs

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. public class ScoreManager : MonoBehaviour
  5. {
  6. public static int score;
  7. Text text;
  8. void Awake ()
  9. {
  10. text = GetComponent <Text> ();
  11. score = 0;
  12. }
  13. void Update ()
  14. {
  15. text.text = "Score: " + score;
  16. }
  17. }

注意这里把Score设置为static变量,使其他 共享一个score并直接调用ScoreManager.score,而不用创建实例

8.给EnemyHealth.ccs添加Score功能:

  1. using UnityEngine;
  2. public class EnemyHealth : MonoBehaviour
  3. {
  4. public int startingHealth = 100;
  5. public int currentHealth;
  6. public float sinkSpeed = 2.5f;
  7. public int scoreValue = 10;//杀死这个enemy获得的分数
  8. public AudioClip deathClip;
  9. ScoreManager ScoreManager;
  10. Animator anim;
  11. AudioSource enemyAudio;
  12. ParticleSystem hitParticles;
  13. CapsuleCollider capsuleCollider;
  14. bool isDead;
  15. bool isSinking;//Enemy死了会下沉,isSinking判断是否在下沉
  16. void Awake ()
  17. {
  18. anim = GetComponent <Animator> ();
  19. enemyAudio = GetComponent <AudioSource> ();
  20. hitParticles = GetComponentInChildren <ParticleSystem> ();
  21. capsuleCollider = GetComponent <CapsuleCollider> ();
  22. ScoreManager = GetComponent<ScoreManager>();
  23. currentHealth = startingHealth;
  24. }
  25. void Update ()
  26. {
  27. if (isSinking)
  28. {
  29. transform.Translate(-Vector3.up * sinkSpeed * Time.deltaTime);
  30. }
  31. }
  32. public void TakeDamage (int amount, Vector3 hitPoint)
  33. {
  34. if (isDead)
  35. return;
  36. currentHealth -= amount;
  37. hitParticles.transform.position = hitPoint;
  38. hitParticles.Play();
  39. if (currentHealth <= 0)
  40. {
  41. Death();
  42. }
  43. }
  44. void Death ()
  45. {
  46. isDead = true;
  47. capsuleCollider.isTrigger = true;
  48. anim.SetTrigger("Dead");
  49. enemyAudio.clip = deathClip;
  50. enemyAudio.Play();
  51. }
  52. public void StartSinking ()
  53. {
  54. GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;//把Enemy的NavMeshAgent关掉
  55. GetComponent<Rigidbody>().isKinematic = true;
  56. //Controls whether physics affects the rigidbody.
  57. //If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.
  58. isSinking = true;
  59. ScoreManager.score += scoreValue;
  60. Destroy(gameObject, 2f);//2秒后消除GameObject
  61. }
  62. }

9.把Bunny变成prefabs方便后面调用

10.完成!


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空