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

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.完成!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值