欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity3D笔记---简单的捕鱼小游戏

程序员文章站 2024-01-24 23:58:04
...

说是捕鱼达人小游戏,其实也没有完全实现捕鱼的功能,只是一个Unity3D前期学习过程中对于Sprite2D的运用,下面我来介绍一下我的界面

Unity3D笔记---简单的捕鱼小游戏

界面组成很简单,主要是生成鱼,以及与的运动,子弹的运动,碰撞检测

生成鱼的脚本,此脚本挂载在一个空物体InitFish上

using UnityEngine;
using System.Collections;

public class InitFish : MonoBehaviour {
    [SerializeField]
    GameObject[] m_FishPrebs;
    //  鱼的数量
    public static int m_Count = 0;
    GameObject m_Fish;
    //  生成鱼的时间
    float m_Timer;
    public float m_TimerCD;
    private void Update()
    {
        m_Timer += Time.deltaTime;
        if (m_Timer>=m_TimerCD)
        {
            m_Timer -= m_TimerCD;
            if (m_Count < 10)
            {
                m_Fish = Instantiate(m_FishPrebs[Random.Range(0, m_FishPrebs.Length)], new Vector3(Random.Range(-9f, 9f), Random.Range(-5f, 5f), 0), Quaternion.identity) as GameObject;
                m_Fish.transform.parent = transform;
                m_Count++;
            }
        }
    }
}

   -->因为鱼的数量有三条,所以最好弄成数组,按照索引随机生成

   -->鱼生成的位置要看你界面的大小,不超过你的界面(可以创建一个空物体去看你的界面的最大X,最小X,最大Y,和最小Y,因为界面是2D的,所以暂时不需要考虑z轴)

   -->鱼的数量设置成静态的原因是,在别的地方也可以调用,让你界面里的鱼永远都不超过10条

鱼的运动,此脚本挂载在所有Fish上

using UnityEngine;
using System.Collections;

public class FishMove : MonoBehaviour {
    public float m_Speed;
    public  float m_Hp;
    //用来判断鱼的方向
    FishDir dir;
    Vector3 target;
    //枚举鱼游动的方向
    enum FishDir
    {
        left,
        rigrt
    }
    void Start()
    {
        //  50%的可能是向左,50%的可能是向右
        float random = Random.value;
        if (random == 0.5f)
        {
            random = Random.value;
        }
        else if (random > 0.5f)
        {
            dir = FishDir.left;
        }
        else
        {
            dir = FishDir.rigrt;
        }
        SetRotation();
        SetTarget();
    }
    //  设置鱼的旋转
    void SetRotation()
    {
        Vector3 scale = transform.localScale;
        if (dir == FishDir.rigrt)
        {
            scale.x = 1;
        }
        else
        {
            scale.x = -1;
        }
        transform.localScale = scale;
    }
    //  设置鱼的目标位置
    void SetTarget()
    {
        //  判断鱼是向左还是向右,向左(0,random.value,0),向右(1,random.value,0)
        float x = dir == FishDir.left ? 0 : 1;
        Vector3 viewPort = new Vector3(x, Random.value, 0);
        target = Camera.main.ViewportToWorldPoint(viewPort+new Vector3(0,0,10));
    }
    private void Update()
    {

        if (Vector3.Distance(transform.position, target) <= 0.8f)
        {
            //  如果鱼的位置接近于边界,鱼向左的话,改为向右,重新设置边界
            dir = dir == FishDir.left ? FishDir.rigrt : FishDir.left;
            SetRotation();
            SetTarget();
        }
        transform.position = Vector3.MoveTowards(transform.position, target, m_Speed * Time.deltaTime);
    }
}


关于炮管的移动,此脚本是挂载在Cannon_0上的

using UnityEngine;
using System.Collections;

public class Cannon : MonoBehaviour {
    /// 功能
    /// 1、根据鼠标的移动而旋转方向
    /// 2、发射子弹
    [SerializeField]
    GameObject m_FirePrefb;
    GameObject m_Fire;
    [SerializeField]
    Transform m_ShootPoint;
    private void Update()
    {

        //  鼠标世界坐标的位置
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10));
        //  大炮到鼠标的方向
        Vector3 CanToMouse = mousePos -transform.position;
        // 大炮需要旋转的度数
        float Euler = Vector3.Angle(Vector3.up,CanToMouse);
        //transform.rotation *= Quaternion.Euler(Euler*Time.deltaTime);

        if (transform.position.x < mousePos.x)
        {
            //鼠标在cannon的右侧
            Euler = -Euler;
        }
        //transform.rotation *= Quaternion.Euler(transform.forward * Euler * Time.deltaTime);
        transform.eulerAngles = new Vector3(0, 0, Euler);

        //  如果点击鼠标,发射子弹
        if (Input .GetMouseButtonDown(0))
        {
            m_Fire = Instantiate(m_FirePrefb, m_ShootPoint.position, transform.rotation) as GameObject;
            Destroy(m_Fire, 3.5f);
        }
    }
}


关于子弹的移动,于碰撞检测

using UnityEngine;
using System.Collections;

public class Fire : MonoBehaviour {
    [SerializeField]GameObject m_ExplosPrefb;
	void Update () {
        transform.position += transform.up * 3f * Time.deltaTime;
	}
    private void OnTriggerEnter2D(Collider2D other)
    {
        //  获得被攻击鱼的血量
        Destroy(gameObject);
        other.GetComponent<FishMove>().m_Hp -= 1;
        //血量小于等于0的时候死亡,放出爆炸特效
        if (other.GetComponent<FishMove>().m_Hp <= 0)
        {
            InitFish.m_Count -= 1;
            Destroy(other.gameObject);
            //  爆炸特效
            GameObject m_Explo=(GameObject) Instantiate(m_ExplosPrefb,other.transform.position,Quaternion.identity);
            Destroy(m_Explo, 2f);
        }
    }
}