• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

125 - Player - PlayerMove.cs - 玩家_移动

作者:

贺及楼

成为作者

更新日期:2023-09-17 11:00:10

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerMove : MonoBehaviour {
  5. public float forward = 0;
  6. private float speed = 3;
  7. private Animator anim;
  8. // Use this for initialization
  9. void Start () {
  10. anim = GetComponent<Animator>();
  11. }
  12. // Update is called once per frame
  13. void FixedUpdate () {
  14. if (anim.GetCurrentAnimatorStateInfo(0).IsName("Grounded") == false) return;
  15. float h = Input.GetAxis("Horizontal");
  16. float v = Input.GetAxis("Vertical");
  17. if (Mathf.Abs(h) > 0 || Mathf.Abs(v) > 0)
  18. {
  19. transform.Translate(new Vector3(h, 0, v) * speed * Time.deltaTime, Space.World);
  20. transform.rotation = Quaternion.LookRotation(new Vector3(h, 0, v));
  21. float res = Mathf.Max(Mathf.Abs(h), Mathf.Abs(v));
  22. forward = res;
  23. anim.SetFloat("Forward", res);
  24. }
  25. }
  26. }