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.

109 lines
2.9 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
4 years ago
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Random = System.Random;
  6. public class PlayerController : MonoBehaviour
  7. {
  8. public float walkSpeed;
  9. public float gravity;
  10. public GameObject model;
  11. public CharacterController cController;
  12. private float speedMulitplier;
  13. private Vector2 receivedInput;
  14. private Vector3 moveDirection = Vector3.zero;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. speedMulitplier = UnityEngine.Random.Range(0.8f, 1.2f);
  19. cController = GetComponent<CharacterController>();
  20. }
  21. public void SetMovement(Vector2 input)
  22. {
  23. receivedInput = input;
  24. }
  25. public void UpdatePosition()
  26. {
  27. float HorseX, HorseZ;
  28. HorseZ = receivedInput.y;
  29. HorseX = receivedInput.x;
  30. if (cController.isGrounded)
  31. {
  32. moveDirection = new Vector3(HorseX, 0, HorseZ);
  33. moveDirection *= walkSpeed * speedMulitplier;
  34. }
  35. moveDirection.y -= gravity * Time.deltaTime;
  36. cController.Move(moveDirection * Time.deltaTime);
  37. //float rotateTo = RotateObject(HorseX, HorseZ);
  38. //HorseZ *= Time.deltaTime * walkSpeed * speedMulitplier;
  39. //HorseX *= Time.deltaTime * walkSpeed * speedMulitplier;
  40. //
  41. //
  42. //transform.Translate(HorseX, 0, HorseZ);
  43. //Vector3 dir = Quaternion.Euler(-90, rotateTo, 0) * Vector3.forward;
  44. //model.transform.forward = dir;
  45. //LeanTween.rotateY(model, rotateTo, 0.1f);
  46. }
  47. public float RotateObject(float xInput, float zInput)
  48. {
  49. float to = model.transform.rotation.eulerAngles.y;
  50. //Debug.Log(to);
  51. if (receivedInput.x != 0 || receivedInput.y != 0)
  52. {
  53. if (zInput > 0 && xInput == 0)
  54. to = 0;
  55. else if (zInput < 0 && xInput == 0)
  56. to = 180;
  57. else if (zInput == 0 && xInput > 0)
  58. to = 90;
  59. else if (zInput == 0 && xInput < 0)
  60. to = 270;
  61. else if (zInput > 0 && xInput > 0)
  62. to = 45;
  63. else if (zInput < 0 && xInput > 0)
  64. to = 135;
  65. else if (zInput < 0 && xInput < 0)
  66. to = 225;
  67. else if (zInput > 0 && xInput < 0)
  68. to = 315;
  69. }
  70. return to;
  71. }
  72. public void MoveObject(float wait)
  73. {
  74. StartCoroutine(RandomWait(wait));
  75. }
  76. public IEnumerator RandomWait(float wait)
  77. {
  78. yield return new WaitForSeconds(UnityEngine.Random.Range(0, wait/2));
  79. if (cController.isGrounded)
  80. {
  81. Vector3 rotateDir = new Vector3(90 * Math.Sign(receivedInput.y), 0, -90 * Math.Sign(receivedInput.x));
  82. model.transform.Rotate(rotateDir, Space.World);
  83. }
  84. }
  85. // Update is called once per frame
  86. void Update()
  87. {
  88. UpdatePosition();
  89. }
  90. }