1.把sense设置为2D模式

2.在HUDCanvas下添加子对象ScoreText

3.设置锚点

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

5.设置ScoreText参数

6.添加Shadow

7.为ScoreText添加脚本ScoreManager.cs

- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
-
- public class ScoreManager : MonoBehaviour
- {
- public static int score;
-
-
- Text text;
-
-
- void Awake ()
- {
- text = GetComponent <Text> ();
- score = 0;
- }
-
-
- void Update ()
- {
- text.text = "Score: " + score;
- }
- }
注意这里把Score设置为static变量,使其他 类 共享一个score并直接调用ScoreManager.score,而不用创建实例
8.给EnemyHealth.ccs添加Score功能:
- using UnityEngine;
-
- public class EnemyHealth : MonoBehaviour
- {
- public int startingHealth = 100;
- public int currentHealth;
- public float sinkSpeed = 2.5f;
- public int scoreValue = 10;//杀死这个enemy获得的分数
- public AudioClip deathClip;
-
- ScoreManager ScoreManager;
- Animator anim;
- AudioSource enemyAudio;
- ParticleSystem hitParticles;
- CapsuleCollider capsuleCollider;
- bool isDead;
- bool isSinking;//Enemy死了会下沉,isSinking判断是否在下沉
-
-
- void Awake ()
- {
- anim = GetComponent <Animator> ();
- enemyAudio = GetComponent <AudioSource> ();
- hitParticles = GetComponentInChildren <ParticleSystem> ();
- capsuleCollider = GetComponent <CapsuleCollider> ();
- ScoreManager = GetComponent<ScoreManager>();
- currentHealth = startingHealth;
- }
-
-
- void Update ()
- {
- if (isSinking)
- {
- transform.Translate(-Vector3.up * sinkSpeed * Time.deltaTime);
- }
- }
-
-
- public void TakeDamage (int amount, Vector3 hitPoint)
- {
- if (isDead)
- return;
- currentHealth -= amount;
- hitParticles.transform.position = hitPoint;
- hitParticles.Play();
-
- if (currentHealth <= 0)
- {
- Death();
- }
- }
-
-
- void Death ()
- {
- isDead = true;
- capsuleCollider.isTrigger = true;
- anim.SetTrigger("Dead");
- enemyAudio.clip = deathClip;
- enemyAudio.Play();
- }
-
-
- public void StartSinking ()
- {
- GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;//把Enemy的NavMeshAgent关掉
- GetComponent<Rigidbody>().isKinematic = true;
- //Controls whether physics affects the rigidbody.
- //If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.
-
- isSinking = true;
- ScoreManager.score += scoreValue;
- Destroy(gameObject, 2f);//2秒后消除GameObject
- }
- }
9.把Bunny变成prefabs方便后面调用

10.完成!
