zoukankan      html  css  js  c++  java
  • unity基础命令

    获取所挂脚本元素的组件:

    rd = GetComponent<Rigidbody>();

    获取其他元素的组件:

    rd = GameObject.Find("Player").GetComponent<Rigidbody>()

    如果查找player下的子游戏物体的transform组件可继续find

    Transform trans=GameObject("Player").transform.find(“zPlayer”)
    transform.find查找子游戏物体的transform组件。

    刚体组件加力效果:

            private Rigidbody rd;
        rd.AddForce(new Vector3(1, 0, 0));

     键盘获取:

    To read an axis use Input.GetAxis with one of the following default axes: "Horizontal" and "Vertical" are mapped to joystick, A, W, S, D and the arrow keys. "Mouse X" and "Mouse Y" are mapped to the mouse delta. "Fire1", "Fire2" "Fire3" are mapped to Ctrl, Alt, Cmd keys and three mouse or joystick buttons. New input axes can be added in the Input Manager.

    以上为api文档中的相关解释,可以通过unity中edit-project settings中进行相关修改。getaxis获取移动轴,获取按键则是getkeydown,入口参数为要按的keycode,返回值为bool。

            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");

    Transform组件:

    Transform组件是每一个游戏物体必存在的属性,所以可以通过transform可以直接获取到脚本所在游戏物体的Transform组件,也可以用gameObject的find方法获取到相应组件,也可以定义一个public 的Transform组件字段,然后把相应组件拖入。在此多说一点采用rigidbody.vector和angularVelocity与rigidbody.translate和rotate产生的效果是一样的。

    碰撞检测与触发检测:

    关于碰撞检测和触发检测的具体定义可以查看api文档,在此只做一下简单说明以及注意事项。碰撞检测为两个物体相撞,会有相撞以后反弹的物理效果,其message void OnCollisionEnter(Collision collision)中入口参数为Collision,表示相撞事件,通过其可以获得与游戏物体相撞的Collider,继而获取其他参数,属性等。当设置为触发器时(即collider的isTriger勾选时)表示此物体不是实体,可以穿越,但是此物体定义了一个范围,当进入此范围时触发消息void OnTriggerEnter(Collider collider),其入口参数即为与游戏物体相撞的物体。

    克隆物体:

     if(Input.GetKeyDown(keycode))
            {
                GameObject.Instantiate(gameobject, trans.position, trans.rotation);
            }

     根据上述基础,贴几段小代码:

    自毁:

    void Start () {
            Destroy(this.gameObject, time);
            
        }

    打击物体:

        void OnTriggerEnter(Collider collider)
        {
            //实例化特效
            Instantiate(shellExplosion, transform.position, transform.rotation);
            //炮弹自毁
            Destroy(this.gameObject);
            //击中物体毁灭
            if(collider.gameObject.tag!="ground")
            {
                Destroy(collider.gameObject);
            }
           // Destroy(collider.gameObject);
        }

    子弹发射:

        private Transform trans;
        //游戏物体子弹
        public GameObject shell;
        public KeyCode key = KeyCode.Space;
        // Use this for initialization
        void Start () {
            // 子弹初始位置对应的游戏物体
            trans = transform.Find("FirePosition");
        }
        
        // Update is called once per frame
        void Update () {
            if(Input.GetKeyDown(key))
            {
                //根据子弹初始位置对应的游戏物体,实例化子弹
                GameObject GO=  Instantiate(shell, trans.position, trans.rotation) as GameObject;
                GO.GetComponent<Rigidbody>().velocity = GO.transform.forward * 15;
            }
            
        }

    未完待续。。。。。。。。。。

  • 相关阅读:
    解决 Windows 资源管理器 CPU 占用率很高的问题
    彻底卸载Cygwin
    Cygwin 快速镜像站点
    解决 cygwin 在 win10 下中文显示乱码的问题
    酷派大神 F1 连接调试,日志获取的方法
    陌路
    游戏开发中自定义脚本与配置
    在 cygwin 的环境下调用 libgdx gdx-tools
    在 cygwin 的环境下使用 git 的时候中文编码的问题
    对 android apk 进行重新签名操作
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/6895487.html
Copyright © 2011-2022 走看看