内容纲要

基本功能

1.玩家根据瞄准点发射子弹。
2.敌人会向玩家这移动。
3.击杀敌人会得分增加。
4.击杀全部敌人就游戏结束。
3D抢滩登陆插图

实现方法

1.玩家的瞄准点是跟着鼠标进行移动

 void Start()
    {
        dir = GetComponent<LineRenderer>();
        dir.SetPosition(0, this.transform.position);
        playerpos = transform.Find("playerpos");
        crossHair = transform.Find("crossHair");
        spawnbullt();
    }

    void spawnbullt()
    {
        GameObject paly1 = Instantiate(bulletprefab, playerpos, false);
        bullet = paly1.transform;

    }

    // Update is called once per frame
    void Update()
    {

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        Debug.DrawRay(ray.origin, ray.direction * 20);
        if (Physics.Raycast(ray, out hit))
        {
            dir.SetPosition(1, hit.point);
            //Vector3 v = transform.position - hit.point; // 从准心指向玩家的方向
            crossHair.position = hit.point;

            Vector3 mousepoint = hit.point;
            mousepoint.y = transform.position.y;//y轴用当前物体的位置;
            mousepoint.x = transform.position.x;//y轴用当前物体的位置;
            mousepoint.z = transform.position.z;//y轴用当前物体的位置;
            //transform.LookAt(mousepoint);
            if (bullet != null && Input.GetMouseButtonDown(0)&&game
                .instance.gv==false)
            {
                Vector3 force = hit.point - bullet.position;
                Rigidbody rb = bullet.GetComponent<Rigidbody>();
                rb.isKinematic = false; //控制物理是否影响刚体
                rb.AddForce(force.normalized * forceMax);
                bullet = null;


                Invoke("spawnbullt", 1f);
            }
        }
    }

2.打中敌人消失:使用碰撞检测方法

void OnCollisionEnter(Collision collision)//碰撞进入
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("bullet"))
        {
            game.instance.AddScore(1);
            Destroy(collision.gameObject);//销毁碰撞物
            Destroy(this.gameObject);


        }
        if (collision.gameObject.layer == LayerMask.NameToLayer("player"))
        {
            game.instance.gv = true;
            game.instance.gameover();
        }

    }

3.通过game脚本控制游戏的逻辑

  public static game instance;
    private int score = 0;
    public Text ScoreText;
    public Text winText;
    public GameObject
        loseText;

    public  bool gv = false;
    public void Awake()
    {
        instance = this;
    }


    public void gameover()
    {

        if (gv == true)
        {

            loseText.SetActive(true);
            SceneManager.LoadScene("Main");
        }
    }


    public void AddScore(int scoreAdd)
    {
        score += scoreAdd;
        ScoreText.text = "score:" + score;

        if (score == 10)
        {
            winText.enabled = true;
        }
    }

1 对 “3D抢滩登陆”的想法;

  1. Pingback: viagra online

发表评论