Unity学习1.1_Unity基础

这一部分内容比较基础,主要是给自己看的,各位高手手下留情,请自动忽略

1、预制体Prefab,可以在游戏中重复使用。

     制作预制体,将场景中的物体拖动到Asset下自动创建

2、摄像机:

选中GameObject->Align with view,当前视角和位置

相关属性:

todo:脚本控制摄像机的移动和旋转,视野FOV等。

3、组件Component:习惯u3d基于组件的开发方式

3.1 移动组件Transform和键盘交互: 

        //键盘交互
        if(Input.GetKey(KeyCode.A))
        {
            Debug.Log("moving");
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("action1");
        }

        if (Input.GetKeyUp(KeyCode.K))
        {
            Debug.Log("action2");
        }
        //鼠标输入 0-> 左键 1-> 右键 2->中键
        if (Input.GetMouseButton(0))
        {
            Debug.Log("左键按住");
        }

        if (Input.GetMouseButtonDown(2))
        {
            Debug.Log("中键按下");
        }

        if (Input.GetMouseButtonUp(1))
        {
            Debug.Log("右键抬起");
        }

// Start is called before the first frame update
    private Transform FT1;
    void Start()
    {
        FT1 = gameObject.GetComponent<Transform>();
    }
    //利用变换组件来移动位置
    //
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            FT1.Translate(Vector3.forward *0.1f,Space.Self);
        }
        if (Input.GetKey(KeyCode.A))
        {
            FT1.Translate(Vector3.left*0.1f,Space.Self);
        }
        if (Input.GetKey(KeyCode.S))
        {
            FT1.Translate(Vector3.back*0.1f,Space.Self);
        }   
        if (Input.GetKey(KeyCode.D))
        {
            FT1.Translate(Vector3.right*0.1f,Space.Self);
        }
    }

3.2 刚体组件:Rigidbody

添加方式:Component->Phsics->Rigidbody

注:刚体组件的移动方式是基于世界坐标系的,会触发物理相关的事件 

private Rigidbody m_Rigidbody;
    private Transform m_Transform;
    // Start is called before the first frame update
    void Start()
    {
        m_Rigidbody = gameObject.GetComponent<Rigidbody>();
        m_Transform = gameObject.GetComponent<Transform>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            m_Rigidbody.MovePosition(m_Transform.position + Vector3.forward*0.1f);
           // m_Transform.Translate(Vector3.forward *0.1f,Space.Self);
        }
        if (Input.GetKey(KeyCode.A))
        {   
            m_Rigidbody.MovePosition(m_Transform.position + Vector3.left*0.1f);
           // m_Transform.Translate(Vector3.left*0.1f,Space.Self);
        }
        if (Input.GetKey(KeyCode.S))
        {
            m_Rigidbody.MovePosition(m_Transform.position + Vector3.back*0.1f);
            //m_Transform.Translate(Vector3.back*0.1f,Space.Self);
        }   
        if (Input.GetKey(KeyCode.D))
        {
            m_Rigidbody.MovePosition(m_Transform.position + Vector3.right*0.1f);
            //m_Transform.Translate(Vector3.right*0.1f,Space.Self);
        }

3.3 碰撞体: 

component- >Physics -> collider

其中mesh collider可以根据选定的网格来生成碰撞体。                 

 ​​​​​​​合理设置rigidbody和collider

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值