项目开发(一)- MMORPG游戏开发实战

  • 代码注释修改
    可以百度,这种代码不需要记忆。一大堆,直接用就可以
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEditor;
public class ScriptsCreat : UnityEditor.AssetModificationProcessor
{
    public static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta", "");
        if (!path.EndsWith(".cs")) return;
        string allText = "// ========================================================\r\n"
                         + "// 描述:\r\n"
                         + "// 作者:雷潮 \r\n"
                         + "// 创建时间:#CreateTime#\r\n"
                         + "// 版 本:1.0\r\n"
                         + "//========================================================\r\n";
        allText += File.ReadAllText(path);
        allText = allText.Replace("#CreateTime#", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        File.WriteAllText(path, allText);
        AssetDatabase.Refresh();
    }
}

效果:每次创建脚本都会有自己的代码注释

效果图
  • 使用角色控制器进行移动
    游戏开发中,Boss与主角的移动都是通过角色控制器进行的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoleCtrl : MonoBehaviour {

    private CharacterController c;

    private Vector3 hitPoint = Vector3.zero;  // 目标点
    private float speed = 10f;

    private Quaternion m_rotation;
    //旋转速度
    private float r_speed = 0.2f;

    private bool isRotationOver = false;

    void Start () {
        c = GetComponent<CharacterController>();
    }
    
    void Update () {

        if (c == null) return;
        //点击屏幕
        if (Input.GetMouseButtonUp(0))   // 
        {
            Debug.Log("鼠标点击屏幕");
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                // 如果碰到了地面,会得到一个点
                if (hitInfo.collider.gameObject.name.Equals("Ground", System.StringComparison.CurrentCultureIgnoreCase))
                {
                    hitPoint = hitInfo.point;
                    isRotationOver = false;
                    r_speed = 0;
                }
             }
        }

        // 如果没有与地面连接,让角色贴着地面
        if (!c.isGrounded)
        {
            c.Move(transform.position + new Vector3(0,-1000,0) - transform.position);
        }


        if (hitPoint != Vector3.zero)
        {
            //Debug.DrawLine(Camera.main.transform.position,hitPoint);

            // 知识点:为什么要判断大于0.1因为移动过程数值中会出现小数。
            if (Vector3.Distance(transform.position,hitPoint) > 0.1)
            {
                //transform.LookAt(hitPoint); // 朝向目标位置,一方面可以进行相关的操作,一方面可以达到某个位置后运动停止,
                //transform.Translate(Vector3.forward*Time.deltaTime*speed,Space.Self);


                Vector3 direction = hitPoint - transform.position;
                direction = direction.normalized; // 归一化,让其在xyz上的值都为1
                direction = direction * Time.deltaTime * speed;
                direction.y = 0;
                // 让角色朝向目标点
                // transform.LookAt(new Vector3(hitPoint.x,transform.position.y,hitPoint.z));
                if (!isRotationOver)
                {
                    r_speed += 5f;
                    // 让角色缓慢转身
                    m_rotation = Quaternion.LookRotation(direction);
                    transform.rotation = Quaternion.Lerp(transform.rotation, m_rotation, Time.deltaTime * r_speed);
                    if (Quaternion.Angle(m_rotation, transform.rotation) < 1f)
                    {
                        r_speed = 1;
                        isRotationOver = true;
                    }
                }
                c.Move(direction);
            }
        }
    }
}
using UnityEngine;

public class RoleCtrl : MonoBehaviour {

    private CharacterController c;

    private Vector3 hitPoint = Vector3.zero;  // 目标点
    private float speed = 10f;

    private Quaternion m_rotation;
    //旋转速度
    private float r_speed = 0.2f;

    void Start () {
        c = GetComponent<CharacterController>();
    }
    
    void Update () {

        if (c == null) return;
        //点击屏幕
        if (Input.GetMouseButtonUp(0))  
        {
            Debug.Log("鼠标点击屏幕");
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                // 如果碰到了地面,会得到一个点
                if (hitInfo.collider.gameObject.name.Equals("Ground", System.StringComparison.CurrentCultureIgnoreCase))
                {
                    hitPoint = hitInfo.point;
                    r_speed = 0;
                }
             }
        }

        // 如果没有与地面连接,让角色贴着地面
        if (!c.isGrounded)
        {
            c.Move(transform.position + new Vector3(0,-1000,0) - transform.position);
        }


        if (hitPoint != Vector3.zero)
        {
            // 知识点:为什么要判断大于0.1因为移动过程数值中会出现小数。
            if (Vector3.Distance(transform.position,hitPoint) > 0.1)
            {
                Vector3 direction = hitPoint - transform.position;
                direction = direction.normalized; // 归一化,让其在xyz上的值都为1
                direction = direction * Time.deltaTime * speed;
                direction.y = 0;
                // 让角色朝向目标点
                // transform.LookAt(new Vector3(hitPoint.x,transform.position.y,hitPoint.z));
                if (r_speed <= 1)
                {
                    r_speed += 5f * Time.deltaTime;
                    // 让角色缓慢转身
                    m_rotation = Quaternion.LookRotation(direction);
                    transform.rotation = Quaternion.Lerp(transform.rotation, m_rotation, r_speed);               
                }
                c.Move(direction);
            }
        }
    }
}
  • 射线检测
    利用射线检测周围的怪物与拾取物体
    什么是射线
检测箱子,可以使用射线方式进行
 if (Input.GetMouseButtonUp(1))
        {
            //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //RaycastHit hit;
            //// 发射射线,检测名称为Item的layer层。
            //if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << LayerMask.NameToLayer("Box")))
            //{
            //    Debug.Log("find box" + hit.collider.name);
            //}

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit[] hitArray = Physics.RaycastAll(ray,Mathf.Infinity,1<<LayerMask.NameToLayer("Box"));
            if (hitArray.Length > 0)
            {
                for (int i = 0; i < hitArray.Length; i++)
                {
                    Debug.Log(hitArray[i].collider.name);
                }
            }
        }
Box检测

检测条件:
(1) - 物体的layer在检测的层中
(2) - 物体身上要有碰撞器 (勾选Trigger触发模式,也可以检测)
注意:
当检测的物体重合,也是可以检测到的,射线具备穿透功能

        if (Input.GetMouseButtonUp(1))
        {
           Collider[] cs =  Physics.OverlapSphere(this.transform.position, 3, 1 << LayerMask.NameToLayer("Box"));
            for (int i = 0; i < cs.Length; i++)
            {
                Debug.Log(cs[i].name);
            }
        }
 private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(this.transform.position, 3);
    }
OnDrawGizmos
检测到Box

触发销毁

using UnityEngine;

public class ScenneCtrl : MonoBehaviour {

    [SerializeField]
    private Transform transBox;
    [SerializeField]
    private Transform parentBox;

    private GameObject boxPrefab;
    private int minCount = 0;
    private int maxCount = 10;

    private float nextCloneTime = 0;
   

    // 数据存储
    private string boxKey = "BoxKey";
    private int getBoxCount;

    void Start () {
        boxPrefab = Resources.Load("BoxPrefabs/Box") as GameObject;
        getBoxCount = PlayerPrefs.GetInt(boxKey,0);
    }
    
    // Update is called once per frame
    void Update () {
        if (minCount < maxCount)
        {
            if (Time.time > nextCloneTime)
            {
                nextCloneTime = Time.time + 3f;
                GameObject cloneObj =   Instantiate(boxPrefab);
                cloneObj.transform.parent = parentBox;
                cloneObj.transform.position = transBox.TransformPoint(new Vector3(UnityEngine.Random.Range(-0.5f,0.5f),0, UnityEngine.Random.Range(-0.5f, 0.5f)));

                BoxController box =  cloneObj.GetComponent<BoxController>();

                if (box != null)
                {
                    box.OnHit = OnHit;
                    minCount++;
                }             
            }
        }
    }

    private void OnHit(GameObject obj)
    {
        minCount--;
        getBoxCount++;
        PlayerPrefs.SetInt(boxKey, getBoxCount);
        GameObject.Destroy(obj);
        Debug.Log("拾取了" + getBoxCount + "个箱子");
    }
}

箱子的代码设置委托,传递自己被点击信息

public class BoxController : MonoBehaviour {

    public System.Action<GameObject> OnHit;

    public void Hit()
    {
        if (OnHit != null)
        {
            OnHit(gameObject);
        }
    }       
}

销毁箱子代码

 if (Input.GetMouseButtonUp(1))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            // 发射射线,检测名称为Item的layer层。
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << LayerMask.NameToLayer("Box")))
            {
                BoxController box = hit.collider.GetComponent<BoxController>();
                if (box != null)
                {
                    box.Hit();
                }
                Debug.Log("find box" + hit.collider.name);

            }
简单实用
  • 摄像机控制
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,101评论 1 32
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,246评论 11 349
  • 首先我们来捋一捋最近值得我们关注的新闻:一是证监会发布新规,余额宝将受新规影响;二是ICO被定性为非法融资;三是金...
    喵叔vip阅读 1,594评论 0 1
  • 长灵河神君最近心情不是很好,按他打算是新鬼的数量越少越好,但越怕什么还越来什么。 前天刚收了一个熊孩子,在接灵殿又...
    一生悫阅读 303评论 0 1
  • 认识良梓是偶然,主持他的《大幕》首发演出,也是偶然。然后,一夜之间就和我喜欢的民谣人结识,并一醉方休。 酒过三巡,...
    一北一川阅读 379评论 5 2