Follower_Script.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Follower_Script : MonoBehaviour
  5. {
  6. GameObject player;
  7. Player_Move_Script moveScript;
  8. private Queue<PlayerState> states = new Queue<PlayerState>();
  9. public int playerLayer = 8;
  10. private float width;
  11. private float height;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. player = GameObject.FindGameObjectWithTag("Player");
  16. moveScript = player.GetComponent<Player_Move_Script>();
  17. width = GetComponent<SpriteRenderer>().bounds.size.x;
  18. height = GetComponent<SpriteRenderer>().bounds.size.y;
  19. }
  20. // Update is called once per frame
  21. void Update()
  22. {
  23. if (Static_GlobalStats.paused) {
  24. float x = player.transform.position.x + (-player.transform.localScale.x * width * 1.15f);
  25. x = (transform.position.x + x) / 2;
  26. float y = (transform.position.y + player.transform.position.y) / 2;
  27. transform.position = new Vector2(x, y);
  28. GetComponent<Animator>().SetFloat("speed", 0);
  29. GetComponent<Animator>().SetBool("grounded", true);
  30. return;
  31. }
  32. if (Mathf.Abs(moveScript.speed) > 0.1f || player.GetComponent<Player_Move_Script>().grounded == false) {
  33. states.Enqueue(new PlayerState(player));
  34. }
  35. int layerMask = ~(1 << playerLayer);
  36. RaycastHit2D groundHit = Physics2D.Raycast(transform.position, Vector2.down, height * 0.6f, layerMask);
  37. if (states.Count > 48 || (!groundHit.collider && states.Count > 0)) {
  38. PlayerState state = states.Dequeue();
  39. transform.position = state.position;
  40. transform.localScale = new Vector2(state.speed == 0 ? transform.localScale.x : Mathf.Abs(state.speed) / state.speed, transform.localScale.y);
  41. GetComponent<Animator>().SetFloat("speed", Mathf.Abs(state.speed));
  42. GetComponent<Animator>().SetBool("grounded", state.grounded);
  43. }
  44. else if (!groundHit.collider) {
  45. float x = player.transform.position.x + (-player.transform.localScale.x * width * 1.15f);
  46. x = (transform.position.x + x) / 2;
  47. float y = (transform.position.y + player.transform.position.y) / 2;
  48. y -= 0.2f;
  49. transform.position = new Vector2(x, y);
  50. GetComponent<Animator>().SetFloat("speed", 0);
  51. GetComponent<Animator>().SetBool("grounded", true);
  52. }
  53. else {
  54. GetComponent<Animator>().SetFloat("speed", 0);
  55. GetComponent<Animator>().SetBool("grounded", groundHit.collider != null);
  56. }
  57. }
  58. private class PlayerState {
  59. public Vector2 position;
  60. public float speed;
  61. public bool grounded;
  62. public PlayerState(GameObject player) {
  63. position = new Vector2(player.transform.position.x, player.transform.position.y);
  64. speed = player.GetComponent<Player_Move_Script>().speed;
  65. grounded = player.GetComponent<Player_Move_Script>().grounded;
  66. }
  67. }
  68. }