当前位置:   article > 正文

unity键盘手柄控制_unity手柄控制代码

unity手柄控制代码

 

 

 

 ​​​​​

 

 

 player movement

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class playermovement : MonoBehaviour
  5. {
  6. private CharacterController characterController;
  7. public float walkspeed = 10f;
  8. public float runSpeed = 15f;
  9. public float speed;
  10. public Vector3 movedirction;
  11. public KeyCode runinputname;
  12. public float jumpforce = 3f;
  13. public Vector3 velocity;
  14. public string jumpinputname = "Jump";
  15. public bool isrun;
  16. public float gravity = -20f;
  17. public bool isrun1;
  18. public bool isjump;
  19. public bool isground;
  20. // Start is called before the first frame update
  21. private void Start()
  22. {
  23. characterController = GetComponent<CharacterController>();
  24. runinputname = KeyCode.LeftShift;
  25. }
  26. // Update is called once per frame
  27. private void Update()
  28. {
  29. Move();
  30. }
  31. public void Move()
  32. {
  33. float h = Input.GetAxis("Horizontal");
  34. float v = Input.GetAxis("Vertical");
  35. movedirction = (transform.right * h + transform.forward * v);
  36. isrun = Input.GetKey(runinputname);
  37. isrun1 = Input.GetButton("LB");
  38. if (isrun || isrun1) { speed = runSpeed; }
  39. else { speed = walkspeed; };
  40. characterController.Move(movedirction * speed * Time.deltaTime);
  41. characterController.Move(velocity * Time.deltaTime);
  42. velocity.y += gravity * Time.deltaTime;//不在地面上(空中,累加重力值);
  43. characterController.Move(velocity * Time.deltaTime); //施加重力;
  44. Jump();
  45. }
  46. public void Jump()
  47. {
  48. isjump = Input.GetButtonDown(jumpinputname);
  49. if (isjump) { velocity.y = Mathf.Sqrt(jumpforce * -2f * gravity); };
  50. }
  51. }

 camera move

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Cameramove : MonoBehaviour
  5. {
  6. public float mouseSensitiviy = 100f;
  7. public Transform playerboby;
  8. public float yRotation = 0f;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. float x = Input.GetAxis("Mouse X")*mouseSensitiviy *Time.deltaTime;
  18. float y = Input.GetAxis("Mouse Y") *mouseSensitiviy *Time.deltaTime ;
  19. float y1 = Input.GetAxis("Mouse Y") ;
  20. float x1 = Input.GetAxis("Mouse X");
  21. yRotation -= y;
  22. if ((Mathf.Abs(y1) > 0.1)&&(yRotation< 70) && (yRotation > -100))
  23. {
  24. transform.localRotation = Quaternion.Euler(yRotation, 0f, 0f);
  25. }
  26. else
  27. { }
  28. if (Mathf.Abs(x1) > 0.1)
  29. { playerboby.Rotate(Vector3.up * x); }
  30. else
  31. { }
  32. //transform.localRotation = Quaternion.AngleAxis(yRotation, Vector3.right);
  33. //playerboby.Rotate(Vector3.up * x);
  34. }
  35. }

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/146546
推荐阅读
相关标签
  

闽ICP备14008679号