找回密码
 立即注册
查看: 257|回复: 0

Unity3D新手入门教程(三)脚本:物体控制,参数,运行,输入

[复制链接]
发表于 2022-5-2 20:29 | 显示全部楼层 |阅读模式
十一 物体的运动


  • 物体的运动
      transform.Translate(dx,dy,dz,...)

  • 相对运动
      tarnsform.Translate(dx,dy,dz,space)space=Space.World|space.Self

  • 运动方向

    • 获取目标物体
        GameObject.Find()

    • 转向目标
        transform.LookAt()z轴指向目标


  • 小练习:达到目标停止
      vector.magnitude获取向量值if语句

十二 物体的旋转


  • 物体旋转
      transform.rotation不建议用transform.eulerAnglestransform.LocalEulerAngles匀速转动

  • 相对旋转
      transform.Rotate()

  • 自转和公转
      公转:围绕另一个物体一起转动

十三 脚本的运行


  • 脚本的运行

    • 场景的加载过程
        创建节点实例化组件实例化脚本组件


  • 消息函数(事件函数,回调函数)
      AwakeStartUpdateOnEnableOnDisable

  • 脚本执行顺序

    • 消息函数调用顺序
        第一阶段初始化Awake()第二阶段Start()帧更新

    • 脚本优先级
        Execution Order


  • 主控脚本
      MainLogic

十四 脚本的参数


  • 脚本参数
      公有数据成员

  • 参数赋值顺序
      类中初始化默认值检查器赋值AwakeStart

  • 值类型

    • 结构体类型
        int,float,boll...Vector3,Color

    • 引用(类)类型
        节点组件资源数组类型


  • 运行时调试保存参数
      play mode下copy componentedit mode下paste component values

十五 鼠标键盘输入


  • 鼠标输入
      Input.GetMouseButtonDown(int)Input.GetMounseButtonUp(int)Input.getMouseButton(int)0左键,1右键,2中键

  • 更多细节
      事件只触发一次状态每帧一次全局事件互不干扰

  • 屏幕坐标

    • 获取屏幕坐标
        Input.mousePosition左下角原点

    • 世界坐标转化屏幕坐标
        Camera.main.WorldToScreenPoint(world pos)

    • 屏幕对象
        Screen.Width


  • 键盘输入
      Input.GetKeyDown(keycode)Input.GetKeyUp(keycode)
    • Input.GetKey(keycode)
        KeyCode.A


附件 脚本汇总

//脚本    //this当前脚本组件继承自MonoBehaviour        //消息函数    void Start()    void Update()    //获取物体,读写其属性    //获取当前物体    GameObject obj = this.gameObject;    //获取场景内其他物体    flag = GameObject.Find("红旗");        //获取位置    //this.gameObject.transform等同于this.transform    Vector3 pos = this.transform.position;    pos = this.transform.localPosition;    //物体运动    //匀速运动:根据速度设置前进距离    distance = speed * Time.deltaTime;    this.transform.Translate(0, 0, distance);        //运动方向    this.transform.LookAt(flag.transform);        //匀速转动    this.gameObject.transform.localEulerAngles += new Vector3(0, rotationSpeed * Time.deltaTime, 0);        float rotationSpeed = 120;    this.transform.Rotate(0, rotationSpeed * Time.deltaTime, 0, Space.Self);    //监听输入    Input.GetMouseButtonDown(0)    Input.GetKey(KeyCode.W)//全局设置    //设置近似帧率    Application.targetFrameRate = 60;        //常用类的静态属性&方法    //时间    Time.time    Time.deltaTime    //屏幕    Screen.width    //输入    Input.mousePosition    //摄像机    Camera.main.WorldToScreenPoint(this.transform.position)
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-22 11:35 , Processed in 0.089222 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表