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.

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