zoukankan      html  css  js  c++  java
  • c#代码总结-雷霆战机

    游戏物体的显示和隐藏  

    GameObject.SetActive(true);

    实现键盘按键功能

    if (Input.GetKey(KeyCode.Mouse0))  //当键盘的0键按下的时候
    { //逻辑判断}

    设置游戏物体的位置

    this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y + speed, this.transform.position.z);

    给参数pos_x一个-3到3的随机值

    float pos_x = Random.Range(-3f,3f);

    Vector3:得到一个位置坐标(x,y,z)

    Vector3 pos = new Vector3(pos_x, this.transform.position.y, this.transform.position.z);//接上,对于随机生成的X坐标建立一个三维位置

    对于Vector3中多个函数可以参看https://www.cnblogs.com/yangwx/p/9051639.html

    instantiate()函数:实例化游戏对象

     对象实例化:https://blog.csdn.net/hyy_sui_yuan/article/details/80916119

    Instantiate(Enemy1, pos, Enemy1.transform.rotation);

    对象间隔时间出现

    public GameObject Enemy0;
    public float startTime_Enemy0 = 0.5f;
    public float rateTime_Enemy1 = 0.2f;
    InvokeRepeating("CreateEnemy0", startTime_Enemy0, rateTime_Enemy0);//startTime之后,每隔rateTime时间,调用一次Fire()方法,创建对象

    鼠标带动游戏物体移动

    原理:把鼠标移动位置,同步给我们游戏物体,并设置游戏的上下左右限制

            if (Input.GetMouseButtonDown(0))//设置开关,当按下左键可以拖动
            {
                isMouse = true;
    
            }
            if (Input.GetMouseButtonUp(0))
            {
                isMouse = false;
                PreviousPos = Vector3.zero;
            }
    
            if (isMouse)
            {
                CurrentPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);// Input.mousePosition屏幕坐标位置,转换成游戏世界当中的位置
                 Vector3 offset = CurrentPos - PreviousPos;
                if (PreviousPos != Vector3.zero)
                {
                    this.transform.position += new Vector3(offset.x, offset.y, 0);//鼠标移动位置同步给Hero
                }
    
                PreviousPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);//Input.mousePosition屏幕坐标位置,转换成游戏世界当中的位置
            }
    
            //左边范围限制
            if (this.transform.position.x<=-x_limit)
            {
                this.transform.position = new Vector3(-x_limit, this.transform.position.y, this.transform.position.z);
            }
            //右边范围限制
            if (this.transform.position.x >= x_limit)
            {
                this.transform.position = new Vector3(x_limit, this.transform.position.y, this.transform.position.z);
            }
            if (this.transform.position.y <= -y_limit)
            {
                this.transform.position = new Vector3(this.transform.position.x, -y_limit, this.transform.position.z);
            }
            if (this.transform.position.y >= y_limit)
            {
                this.transform.position = new Vector3(this.transform.position.x, y_limit, this.transform.position.z);
            }
    
        }

    对象删除

    GameObject gOj;
        void Start()
        {
            //删除对象类方法(有两种重载形式)
            ///Destroy(对象名);
            ///Destroy(对象名 , 延时值);
            Destroy(gameObject); //直接删除对象
            Destroy(gOj, 10f);  //延时10秒后删除
        }

    刚体碰撞 先将属性设置为刚体

    void OnTriggerEnter2D(Collider2D collider2d)
        {
            if(collider2d.gameObject.CompareTag("Enemy"))
            {
                //collider2d=Enemy GameObject
                //给敌机造成伤害
                //SendMessage
                collider2d.gameObject.SendMessage("BeHit");
    
                Destroy(this.gameObject);
            }
        }
  • 相关阅读:
    Linux下暴力破解工具Hydra详解
    LeetCode OJ--Evaluate Reverse Polish Notation
    LeetCode OJ--Valid Parentheses
    LeetCode OJ--Implement strStr()
    LeetCode OJ--Valid Palindrome
    LeetCode OJ--Remove Duplicates from Sorted List II *
    LeetCode OJ--Partition List
    LeetCode OJ--Reverse Linked List II
    LeetCode OJ--3Sum **
    LeetCode OJ--Search in Rotated Sorted Array II
  • 原文地址:https://www.cnblogs.com/sylvia1111/p/14420309.html
Copyright © 2011-2022 走看看