前言:

 这一篇章实现物理碰撞,就是游戏体碰撞减装甲,这几天想要试着做出兼具装甲与血量的模式,可自动回复的装甲与永久损伤的血量,在一些平台上找到了不少有意思的模型,有兴趣的可以自己找找模型替换一下。

 

射击类游戏实例

 

作为第一个用来发布讲解的游戏,我立马就想到了射击类游戏,当然不是第一人称射击的那种,是打小飞机累计得分的那种类型,方便魔改参数以及自行制作一些敌人的模型。

游戏相关设定:

1.在游戏中,我们将操作战舰击坠敌人的飞船,游戏开始后战舰会向前推进,消灭敌人取得分数,战舰被击落游戏才会结束。

2.战舰拥有固定装甲(血量),敌人有多种并拥有独特的飞行轨迹与装甲

3.屏幕上会显示血量、得分等内容

4.待添加

 

涉及的英文:

enemy:敌人         box collider:盒碰撞器                  physics:物理                  Gravity: 重力           Rigidbody:刚体             Kinematic:运动学的       Trigger: 触发       

 

 

介绍:

1.UpdaMove函数用来执行敌人的移动,使用了Sin函数使数值在-1~1之间循环往复实现往复运动。

2.Time.time是游戏的进行时间。

3.other.tag=="PlayerRocket"比较字符串判断碰撞体是否为主角子弹。

4.Rocket rocket=other.GetComponent<Rocket>()语句获得了对方碰撞体的Rocket脚本组件。

5.m_life-=rocket.m_power语句会逐步减少装甲,到0时使用Destory消除游戏体。

 

 

操作:

1.创建Enemy.cs脚本,编写代码实现敌人游戏体的移动

protected virtual void UpdateMove()
    {
        float rx = Mathf.Sin(Time.time) * Time.deltaTime;

        transform.Translate(new Vector3(rx, 0, -m_speed * Time.deltaTime));


    }

 

2.建立敌人游戏体的prefab,并将Enemy脚本指定给它

 

3.给敌人游戏体添加碰撞体,【Component】—【Physics】—【Box Collider】,在Inspector窗口找到【Is Trigger】,勾选上

 

 

4.添加刚体组件,【Component】—【Physics】—【Rigidbody】,取消【Use Gravity】,勾选【Is Kinematic】

 

 

5.给主角重复上述操作

6.【Edit】—【Project Settings】—【Tags and Layers】,新建新的Tag,PlayerRocket和Enemy,选中敌人的prefab修改tag为Enemy,子弹的tag为PlayerRocket,主角的tag为Player(内置的没有就自己创建)

 

 

 7.打开Rocket.cs编写代码实现子弹的碰撞消失

 private void OnTriggerEnter(Collider other)
    {
        if (other.tag != "Enemy")
        {
            return;

        }
        else
        {
            Destroy(this.gameObject);
        }
    }

 

8.打开Player.cs编写代码实现主角的碰撞消失

private void OnTriggerEnter(Collider other)
    {
        if (other.tag != "PlayerRocket"){
            m_life -= 1;
            if (m_life <= 0)
            {
                Destroy(this.gameObject);
            }

        }
    }

 

9.打开Enemy.cs编写代码实现敌人的碰撞消失与飞出屏幕外自我消失

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[AddComponentMenu("MyGame/Enemy")]
public class Enemy : MonoBehaviour
{
    public float m_speed = 1;
    public float m_life = 10;
    protected float m_rotspeed = 30;

   public Renderer m_renderer;
   internal bool m_isActiv = false;


    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "PlayerRocket")
        {
            rocket rocket = other.GetComponent<rocket>();
            if (rocket != null)
            {
                m_life -= rocket.m_power;
                if (m_life <= 0)
                {
                    Destroy(this.gameObject);
                }

            }
        }
        else if (other.tag == "Player")
        {
            m_life = 0;
            Destroy(this.gameObject);
        }
    }
    // Start is called before the first frame update
    void Start()
    {

        m_renderer = this.GetComponent<Renderer>();




    }

    private void OnBecameVisible()
    {
        m_isActiv = true;
    }

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

        if (m_isActiv && !this.m_renderer.isVisible)  // 如果移动到屏幕外
        {
            Destroy(this.gameObject); // 自我销毁
        }
    }

    protected virtual void UpdateMove()
    {
        float rx = Mathf.Sin(Time.time) * Time.deltaTime;

        transform.Translate(new Vector3(rx, 0, -m_speed * Time.deltaTime));


    }




}

 

 

再说一句:

突然发现很多简单预置函数没有说明,比如说Vector3,之后涉及多了另说明吧,另外像创建碰撞体时候涉及到重力之类的,后面有用到的具体实例说起来应该会很简单,

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/zzuadj/p/14166474.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!