You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerController : MonoBehaviour
  5. {
  6. public float walkSpeed;
  7. public GameObject model;
  8. private Vector2 receivedInput;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. StartCoroutine((FlipOnAxis()));
  13. }
  14. public void SetMovement(Vector2 input)
  15. {
  16. receivedInput = input;
  17. }
  18. public void UpdatePosition()
  19. {
  20. float HorseX, HorseZ;
  21. HorseZ = Input.GetAxisRaw("Vertical");
  22. HorseX = Input.GetAxisRaw("Horizontal");
  23. float rotateTo = RotateObject(HorseX, HorseZ);
  24. HorseZ = Input.GetAxis("Vertical") * Time.deltaTime * walkSpeed;
  25. HorseX = Input.GetAxis("Horizontal") * Time.deltaTime * walkSpeed;
  26. transform.Translate(HorseX, 0, HorseZ);
  27. LeanTween.rotateY(model, rotateTo, 0.1f);
  28. }
  29. public float RotateObject(float xInput, float zInput)
  30. {
  31. float to = model.transform.rotation.eulerAngles.y;
  32. //Debug.Log(to);
  33. if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
  34. {
  35. if (zInput > 0 && xInput == 0)
  36. to = 0;
  37. else if (zInput < 0 && xInput == 0)
  38. to = 180;
  39. else if (zInput == 0 && xInput > 0)
  40. to = 90;
  41. else if (zInput == 0 && xInput < 0)
  42. to = 270;
  43. else if (zInput > 0 && xInput > 0)
  44. to = 45;
  45. else if (zInput < 0 && xInput > 0)
  46. to = 135;
  47. else if (zInput < 0 && xInput < 0)
  48. to = 225;
  49. else if (zInput > 0 && xInput < 0)
  50. to = 315;
  51. }
  52. return to;
  53. }
  54. public IEnumerator FlipOnAxis()
  55. {
  56. if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
  57. {
  58. LeanTween.rotateX(model, -90, 0.1f);
  59. }
  60. yield return new WaitForSeconds(0.5f);
  61. }
  62. }
  63. // Update is called once per frame
  64. void Update()
  65. {
  66. UpdatePosition();
  67. }
  68. }