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.

76 lines
2.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class LocalPlayer : Player {
  5. public CharacterController charControl;
  6. public Animator Animator;
  7. public float Speed = 5;
  8. private float animWalkSpeed = 0;
  9. protected override void Update()
  10. {
  11. ApplyGravity();
  12. ApplyInput();
  13. Teleport();
  14. base.Update();
  15. }
  16. private void ApplyGravity()
  17. {
  18. if (!charControl.isGrounded)
  19. {
  20. charControl.SimpleMove(new Vector3(0.0f, -5.0f * Time.deltaTime, 0.0f));
  21. }
  22. }
  23. private void ApplyInput()
  24. {
  25. if (Input.GetMouseButton(0))
  26. {
  27. Vector3 mousePos = new Vector3(Input.mousePosition.x, 0.0f, Input.mousePosition.y);
  28. Vector3 screenCentre = new Vector3(Screen.width / 2, 0.0F, Screen.height / 2);
  29. Vector3 movePos = mousePos - screenCentre;
  30. movePos.x = movePos.x / Screen.width;
  31. movePos.z = movePos.z / Screen.height;
  32. charControl.Move(movePos * Speed * Time.deltaTime);
  33. animWalkSpeed = Mathf.Clamp01(movePos.magnitude * 3);
  34. playerModel.transform.LookAt(playerModel.transform.position + movePos, Vector3.up);
  35. }else if (animWalkSpeed > 0)
  36. {
  37. animWalkSpeed -= 0.1f;
  38. }
  39. Animator.SetFloat("WalkSpeed", animWalkSpeed);
  40. }
  41. void Teleport()
  42. {
  43. if (map == null)
  44. return;
  45. if (transform.position.x > map.maxX)
  46. {
  47. transform.position = new Vector3(map.minX, transform.position.y, transform.position.z);
  48. }
  49. else if (transform.position.x < map.minX)
  50. {
  51. transform.position = new Vector3(map.maxX, transform.position.y, transform.position.z);
  52. }
  53. if (transform.position.z > map.maxZ)
  54. {
  55. transform.position = new Vector3(transform.position.x, transform.position.y, map.minZ);
  56. }
  57. else if (transform.position.z < map.minZ)
  58. {
  59. transform.position = new Vector3(transform.position.x, transform.position.y, map.maxZ);
  60. }
  61. }
  62. }