经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
Unity 游戏开发、03 基础篇 | C#初级编程
来源:cnblogs  作者:小能日记  时间:2023/9/15 13:31:24  对本文有异议

C#初级编程

https://learn.u3d.cn/tutorial/beginner-gameplay-scripting

8 Update 和 FixedUpdate

Update(不是按固定时间调用的) 经常用于

  • 移动非物理特性的物体(不是物理对象)
  • 简单定时器
  • 接收输入

FixedUpdate(调用时间间隔相同)

  • 进行任何必要的物理计算(任何物理对象)
    • 最好使用力来定义移动

使用 IDE 的 Unity Event Functions 插入函数


9 点积、叉积

image-20230914192214954

点积算出标量如果点积为 0 则两个向量互相垂直,飞机模拟例子:

  • 点积 = 0,飞机前向 Z 轴 与 Y 轴垂直,此时阻力最小
  • 点积 > 0,飞机在爬升,此时阻力变大
  • 点积 < 0,飞机在俯冲

image-20230914192741074 image-20230914192825341 image-20230914193015463

叉积算出新向量。使用左手坐标系根据 A 、B 确定 C 的方向(中指)

  • 为了确定围绕哪个轴施加扭矩 来转动坦克炮塔
  • 如果知道炮塔目前朝向目标朝向,则可以用叉积算出需要围绕的轴的方向
image-20230914193323362

10 启用和禁用组件

  1. private Light myLight;
  2. private void Start()
  3. {
  4. myLight = GetComponent<Light>();
  5. }
  6. private void Update()
  7. {
  8. if (Input.GetKeyUp(KeyCode.Space))
  9. myLight.enabled = !myLight.enabled;
  10. }

11 激活游戏对象

  • 父节点无效后,子节点也会一同跟着无效,(不活跃节点在层次结构中依旧可见)
    • 利用父节点保持对对象群的控制
    • 子节点由于父节点被禁用而禁用时,只针对子节点setActive不管用,想要重新激活子节点就必须重新激活父节点
  • 要确认某个节点是否为活跃状态
    • activeSelf 看节点本身
    • activeInHierarchy 看父子关系(父节点关则false;根节点自身为父节点)
  1. Debug.Log(myObject.activeSelf);
  2. Debug.Log(myObject.activeInHierarchy);
image-20230914200507451

12 物体移动

  • 默认移动,旋转都是 Space.self
  • 移动一个有碰撞体的对象(带物理效果的物体),不能用 Translate 与 Rotate
  1. public void Rotate(Vector3 axis, float angle) => this.Rotate(axis, angle, Space.Self);

13 LookAt

让摄像机看向某一物体

  1. public Transform target;
  2. private void Update()
  3. {
  4. transform.LookAt(target);
  5. }

14 Lerp

线性插值会在两个给定值之间找到某个百分比的值。

  1. float result = Mathf.Lerp (3f, 5f, 0.5f);
  2. // Color.Lerp 和 Vector3.Lerp
  3. Vector3 from = new Vector3 (1f, 2f, 3f);
  4. Vector3 to = new Vector3 (5f, 6f, 7f);
  5. // 此处 result = (4, 5, 6)
  6. Vector3 result = Vector3.Lerp (from, to, 0.75f);

在对值进行平滑时,通常情况下最好使用 SmoothDamp 函数。想要实现特定效果时,才应使用 Lerp 进行平滑。

Unity - Scripting API: Mathf.SmoothDamp (unity3d.com)

Need help understanding the Mathf.SmoothDamp function - Unity Forum

ref velocity 存在的必要性

如果目标距离较远,函数会尝试加速你的位置,如果目标越来越近,则会减速。为了加速/减速,你需要知道你的速度有多快,这就是速度参数


16 GetButton、GetKey

  • GetKey 需要使用 KeyCode 类型参数
  • KeyCode 只与特定键相关,建议使用 GetButton
    • 参数是字符串,可以在 Project Settings | Input Manager 中设置,灵活性很大
  • 两种方式都有三种状态:Down、状态、Up
  1. private void Update()
  2. {
  3. if(Input.GetButtonDown("Jump"))
  4. Debug.Log("按下了空格键");
  5. if(Input.GetButtonDown("Fire1"))
  6. Debug.Log("按下了鼠标左键");
  7. }

17 GetAxis、GetAxisRaw

  • GetButton、GetKey 只返回布尔值
  • GetAxis 返回浮点值,介于-1到1之间
  • GetAxisRaw  返回浮点值,仅返回 -1,0,1,不受平滑过滤参数影响
    • 针对需要严格控制输入的2D游戏非常有用
image-20230914205224260
  • Gravity 按钮松开后归零的速度(越高归零越快)
  • Sensitivity 按钮按下后到达1或-1的速度有多快(越高越快)
  • Dead 值针对手柄盲区(LS、RS轻微转动不影响)(越大盲区越大)
  1. private void Update()
  2. {
  3. float x = Input.GetAxis("Horizontal");
  4. float v = Input.GetAxis("Vertical");
  5. float x2 = Input.GetAxisRaw("Horizontal");
  6. float v2 = Input.GetAxisRaw("Vertical");
  7. Debug.Log($"{x} {v} {x2} {v2}");
  8. }

  1. void Movement ()
  2. {
  3. float forwardMovement = Input.GetAxis("Vertical") * speed * Time.deltaTime;
  4. float turnMovement = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
  5. transform.Translate(Vector3.forward * forwardMovement);
  6. transform.Rotate(Vector3.up * turnMovement);
  7. }

18 OnMouseDown

用于检测对 带碰撞体组件的物体节点GUI元素 鼠标点击事件

以下代码针对一个物理物体,每次鼠标点击给予 forward 反方向的推力

  1. private void OnMouseDown()
  2. {
  3. Debug.Log($"点击 {gameObject.name}");
  4. rb.AddForce(-transform.forward * 500f);
  5. }

19 GetComponent

GetComponent 性能消耗大,应该尽量减少调用,最好是在 Awake,Start 调用


20 deltaTime

定时器功能与平滑移动(按速度移动)

  1. public float speed = 8f;
  2. public float countdown = 3.0f;
  3. void Update ()
  4. {
  5. countdown -= Time.deltaTime;
  6. if(countdown <= 0.0f)
  7. light.enabled = true;
  8. if(Input.GetKey(KeyCode.RightArrow))
  9. transform.position += new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
  10. }

21 值、引用

image-20230914213217303

  • 值类型对应的值被改变,只会影响特定变量
  • 引用类型指向的值被改变,所有包含相同存储地址的引用类型变量都会受到影响
  1. void Start ()
  2. {
  3. //值类型变量
  4. Vector3 pos = transform.position; // 值拷贝
  5. pos = new Vector3(0, 2, 0);
  6. //引用类型变量
  7. Transform tran = transform;
  8. tran.position = new Vector3(0, 2, 0);
  9. }

22 类

  • 拆分类的功能,不要全部写在一起(单一职责原则)
    • 如射击类、移动类、库存类
  • 多用代码段,比如 ctor 生成构造函数
  • 先全面仔细设计脚本结构,再开始编写一个大类,将各种不同内容囊括其中

23 Instantiate

Instantiate 用于动态生成预制体对象(创建预制件的克隆体)

?动态实例化生成的子弹预制体节点被强制转型成了 RigidBody

  1. public Rigidbody bulletPrefab; // 可以不是GameObject而是绑定的组件
  2. public Transform firePosition;
  3. public float bulletSpeed;
  4. ...
  5. Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, firePosition.rotation) as Rigidbody;

案例中用到了 as 关键字。《C# 8.0本质论》里没有写

  • 按(Rigidbody)显式转换方式如果转换不成功会抛出异常
  • as 运算符类似于强制转换操作;但是,如果转换不可行,as 会返回 null 而不是引发异常

24 数组

使用 FindGameObjectsWithTag 初始化查找所有 Tag 为 Player 的节点存储至数组中

C# 数组是从抽象的基类型Array派生的引用类型

  1. public GameObject[] players;
  2. void Start()
  3. {
  4. players = GameObject.FindGameObjectsWithTag("Player");
  5. foreach (var player in players)
  6. {
  7. Debug.Log($"{player.name}");
  8. }
  9. }

25 Invoke

  • Invoke 调用的函数必须 不包含参数,且无返回值

其他内容以前笔记有写,略过


26 enum

用整数描述方向不易读,可以建立 enum 类型(类内类外都可创建

  1. enum Direction : short
  2. {
  3. North, // 0
  4. East, // 1
  5. South = 100, // 100
  6. West // 101
  7. };
  8. void Start()
  9. {
  10. Direction myDirection;
  11. myDirection = Direction.North;
  12. var newDirection = ReverseDirection(myDirection);
  13. }
  14. Direction ReverseDirection (Direction dir)
  15. {
  16. if(dir == Direction.North)
  17. dir = Direction.South;
  18. else if(dir == Direction.South)
  19. dir = Direction.North;
  20. else if(dir == Direction.East)
  21. dir = Direction.West;
  22. else if(dir == Direction.West)
  23. dir = Direction.East;
  24. return dir;
  25. }

27 switch

跟 C++ 差不多

  1. public int intelligence = 5;
  2. void Start()
  3. {
  4. switch (intelligence)
  5. {
  6. case 1:
  7. break;
  8. case 2:
  9. break;
  10. default:
  11. break;
  12. }
  13. }

原文链接:https://www.cnblogs.com/linxiaoxu/p/17703722.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号