zoukankan      html  css  js  c++  java
  • Unity3D 5.x 简单实例

    1,下载、安装: http://unity3d.com/cn/get-unity/download/archive 

      建议直接借助 UnityDownloadAssistant 进行安装,根据需要勾选需要的资源包,下载完成后有以下资源:

      

    2,学习视频:51自学网 → Unity3D游戏制作入门教程

       学习建议: 参考教程中的思路、实现方法,但不一定完全要按照教程中的每一步去做,觉得不好的地方,自己可以变通一下

    3,发射炮弹实例注意事项:

      (1) 教学中编辑器是4.X版本的,官网下载的最新版本是5.X版本,部分Javascript的代码语法不同

        如:5.X 版本中 获取组件的代码是:  n.GetComponent(Rigidbody).AddForce(fwd*3800);

          5.X 版本中 可用 UI - Text 代替 之前版本的 GUIText :  

          gameObject.Find("Canvas/Text").GetComponent(Text).text ="射击次数:"+shootNums;  

      (2) 地面建议用 3D Object →  Plane 对象,然后加大 x,z ,y 设置为1 

      (3) 显示发射次数和显示次数的文本 

      (4) 发射的子弹暂时不Destroy

    4,以下是1-21课完整的Javascript代码:

       附加在Main Camera上的JS: 

    import UnityEngine.UI;
    
    #pragma strict
    
    var speed:int=5;
    var Newobject:Transform;
    
    //发射次数
    var shootNums:int=1000;
    //保存消灭数量
    var  killNums:int=0;
    
    function Start () {
        gameObject.Find("Canvas/Text").GetComponent(Text).text ="射击次数:"+shootNums + "  消灭数量:"+killNums;   
    
        //加载音乐
        gameObject.Find("Plane").GetComponent(AudioSource).Play();
        gameObject.Find("bullt").GetComponent(AudioSource).Pause();
    }
    
    
    function Update () {
    
        //镜头(视角)前后左右移动 : W S A D
        var x:float=Input.GetAxis("Horizontal")*Time.deltaTime*speed;
        var z:float=Input.GetAxis("Vertical")*Time.deltaTime*speed;
        transform.Translate(x,0,z);
           
       
        //点击 Ctrl 或 点击鼠标左键 发射
        if (Input.GetButtonDown("Fire1")) { 
    
            //创建发射球体
            var n:Transform =  Instantiate(Newobject,transform.position,transform.rotation);
            //定义发射方向
            var fwd:Vector3=transform.TransformDirection(Vector3.forward);
            //发射力度
            n.GetComponent(Rigidbody).AddForce(fwd*3800);
    
            gameObject.Find("bullt").GetComponent(AudioSource).Play();
            //记录发射次数
            shootNums+=1;  
             
            //更新发射次数
            gameObject.Find("Canvas/Text").GetComponent(Text).text ="射击次数:"+shootNums+ "  消灭数量:"+killNums; 
        }  
    
        //旋转功能
        //视角向右转 E
        if(Input.GetKey(KeyCode.Q)){
            transform.Rotate(0,-25*Time.deltaTime,0,Space.Self);
        }
        //视角向左转 Q
        if(Input.GetKey(KeyCode.E)){
            transform.Rotate(0,25*Time.deltaTime,0,Space.Self);
        }
        //仰视 Z
        if(Input.GetKey(KeyCode.Z)){
            transform.Rotate(-25*Time.deltaTime,0,0,Space.Self);
        }
        //俯视 C
        if(Input.GetKey(KeyCode.C)){
            transform.Rotate(25*Time.deltaTime,0,0,Space.Self);
        }
    
        //镜头水平向上、水平向下移动
        if(Input.GetKey(KeyCode.H)){
            transform.Translate(0,5*Time.deltaTime,0);
        }
        if(Input.GetKey(KeyCode.N)){
            transform.Translate(0,-5*Time.deltaTime,0);
        }
    }
    View Code

        附加在消灭立方体物体上的JS:   

    #pragma strict
    
    function Start () {
    
    } 
    
    //消灭数量
    var countkillNums:int=0;
    
    function Update () {   
         
        if (gameObject.transform.position.y<0) {   
    
            countkillNums=++gameObject.Find("Main Camera").GetComponent(ShootBullt).killNums;   
    
            if (countkillNums>=21) {
                gameObject.Find("Canvas/Text").GetComponent(Text).text =   "恭喜:闯关成功!" ;  
                gameObject.Find("Main Camera").GetComponent(ShootBullt).enabled=false; 
            }
    
            Destroy(gameObject);
        }
      
    }
    View Code

    实现效果

      

    5.x KEY : Download   Reference

  • 相关阅读:
    Http Requests for PHP
    关于ORA-00979 不是 GROUP BY 表达式错误的解释
    boke例子: freermarker:在使用ajax传递json数据的时候多出冒号
    boke练习: springboot整合springSecurity出现的问题,传递csrf
    boke练习: springboot整合springSecurity出现的问题,post,delete,put无法使用
    feign三:覆写feign的默认配置及feign的日志
    boke练习: spring boot: security post数据时,要么关闭crst,要么添加隐藏域
    boke练习: freemarker对空变量报错 (classic_compatible设置true,解决报空错误)
    mysql查询、子查询、连接查询
    MySQL Group By 实例讲解(二)
  • 原文地址:https://www.cnblogs.com/i-shanghai/p/5548620.html
Copyright © 2011-2022 走看看