• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

36 - C# - 移动视角 - transform

作者:

贺及楼

成为作者

更新日期:2024-05-09 19:43:09

作用:通过键盘移动视角transform
键盘wasd — 移动相机

代码

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class movement : MonoBehaviour
  5. {
  6. public float speed = 5;
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. }
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. float h = Input.GetAxis("Horizontal");
  15. float l = Input.GetAxis("Vertical");
  16. transform.Translate(new Vector3(h,l,0)*Time.deltaTime * speed);
  17. }
  18. }

代码解释

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class movement : MonoBehaviour
  5. {
  6. public float speed = 5;//移动速度
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. }
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. float h = Input.GetAxis("Horizontal");//上下方向键w、s
  15. float l = Input.GetAxis("Vertical");//左右方向键a、d
  16. transform.Translate(new Vector3(h,l,0)*Time.deltaTime * speed);//按时间、速度定义,(0,0,0)对应(x,y,z)轴
  17. }
  18. }

使用

1.把脚本拉到要移动的物体