unity 触碰器类

在项目中只用于写碰撞器等操作

using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using UnityEngine;
using UnityEngine.UI;

/*
*创建者:
*创建时间:
*描述:
*版本:
*/
public class PlayerTirgger : MonoBehaviour
{
    private AudioSource audios;
    private AudioClip clip, doorOpen,NoKeyDoor;//声音管理

    private GameObject door,win;//第一关door门,win最后胜利的门
    private GameObject gameOver;//游戏结束画面
    private bool doorSetActive=false;//第一关门
    private bool OpenDoor = true;//打开胜利的钥匙
    private Text tap;
    private GameObject[] player;
    private float nextTime=0;
    // Start is called before the first frame update
    void Start()
    {
        audios= GetComponent<AudioSource>();
        clip = Resources.Load<AudioClip>(GameResources.GetKeyCard);
        doorOpen = Resources.Load<AudioClip>(GameResources.DoorOpen);
        NoKeyDoor = Resources.Load<AudioClip>(GameResources.NoDoorOpen);
        door = GameObject.FindWithTag("Door");
        win = GameObject.FindWithTag("Win");

        
        tap = GameObject.Find("GameOver").transform.GetChild(0).GetComponent<Text>();
        
    }

    // Update is called once per frame
    void Update()
    {
        
        if (doorSetActive)//第一关门的落下显示
        {
            nextTime += Time.deltaTime;
            if (nextTime>=3)
            {
                door.gameObject.SetActive(true);
                nextTime = 0;
                doorSetActive = false;
            }
        }
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("KeyCard"))
        {
            
            audios.clip = clip;
            GameManager.Instance().keyCard = true;
            audios.Play();
            Destroy(collision.gameObject);
            print(GameManager.Instance().keyCard);
        }
        if (collision.gameObject.CompareTag("Door") && GameManager.Instance().keyCard)//获取到Card并靠近门时,开门
        {
            collision.gameObject.SetActive(false);//挡住的门消失
            audios.clip = doorOpen;
            audios.Play();
            doorSetActive = true;//开启倒计时,把门出现

        }
        else if (collision.gameObject.CompareTag("Door") && GameManager.Instance().keyCard == false)//没有获取到Card并靠近门时
        {
            audios.clip = NoKeyDoor;
            if (!audios.isPlaying)//警报声响起
            {
                audios.Play();
            }
            GameManager.Instance().PlayerHp -= 1;//玩家持续扣血           
        }
        else if (collision.gameObject.CompareTag("Door"))//其他的操作
        {

        }
        if (collision.gameObject.CompareTag("WinCard"))
        {
            audios.clip = clip;
            GameManager.Instance().WinCard = true;
            audios.Play();
            Destroy(collision.gameObject);
        }
        if (collision.gameObject.CompareTag("Win") && GameManager.Instance().WinCard&&GameManager.Instance().feng>=50)
        {
            tap.text = "恭喜你游戏胜利!!!";            
            GetComponent<MouseLook>().enabled = false;
            if (GameObject.Find("Player1")!=null)
            {
                GetComponent<PlayerACR>().enabled = false;
            }
            if (GameObject.Find("G36C@Draw")!=null)
            {
                GetComponent<PlayerTwoG36C>().enabled = false;
            }
            
            Time.timeScale = 0;
            

        }
        else if (collision.gameObject.CompareTag("Win") && GameManager.Instance().WinCard == false)
        {
          
                tap.text = "请获取钥匙再开门";
                Invoke("tapTxt", 3f);
            
        }
        else if (collision.gameObject.CompareTag("Win") && GameManager.Instance().WinCard == true&& GameManager.Instance().feng < 50)
        {
            tap.text = "请累积积分至50分";
            Invoke("tapTxt", 3f);
        }
        else if (collision.gameObject.CompareTag("Win"))//其他的操作
        {

        }
    }
   
    void tapTxt()
    {
        tap.text = "";
    }
    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值