PlatformerCharacter2D.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using UnityEngine;
  3. #pragma warning disable 649
  4. namespace UnityStandardAssets._2D
  5. {
  6. public class PlatformerCharacter2D : MonoBehaviour
  7. {
  8. [SerializeField] private float m_MaxSpeed = 10f; // The fastest the player can travel in the x axis.
  9. [SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
  10. [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
  11. [SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
  12. [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
  13. private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
  14. const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
  15. private bool m_Grounded; // Whether or not the player is grounded.
  16. private Transform m_CeilingCheck; // A position marking where to check for ceilings
  17. const float k_CeilingRadius = .01f; // Radius of the overlap circle to determine if the player can stand up
  18. private Animator m_Anim; // Reference to the player's animator component.
  19. private Rigidbody2D m_Rigidbody2D;
  20. private bool m_FacingRight = true; // For determining which way the player is currently facing.
  21. private void Awake()
  22. {
  23. // Setting up references.
  24. m_GroundCheck = transform.Find("GroundCheck");
  25. m_CeilingCheck = transform.Find("CeilingCheck");
  26. m_Anim = GetComponent<Animator>();
  27. m_Rigidbody2D = GetComponent<Rigidbody2D>();
  28. }
  29. private void FixedUpdate()
  30. {
  31. m_Grounded = false;
  32. // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
  33. // This can be done using layers instead but Sample Assets will not overwrite your project settings.
  34. Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
  35. for (int i = 0; i < colliders.Length; i++)
  36. {
  37. if (colliders[i].gameObject != gameObject)
  38. m_Grounded = true;
  39. }
  40. m_Anim.SetBool("Ground", m_Grounded);
  41. // Set the vertical animation
  42. m_Anim.SetFloat("vSpeed", m_Rigidbody2D.velocity.y);
  43. }
  44. public void Move(float move, bool crouch, bool jump)
  45. {
  46. // If crouching, check to see if the character can stand up
  47. if (!crouch && m_Anim.GetBool("Crouch"))
  48. {
  49. // If the character has a ceiling preventing them from standing up, keep them crouching
  50. if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
  51. {
  52. crouch = true;
  53. }
  54. }
  55. // Set whether or not the character is crouching in the animator
  56. m_Anim.SetBool("Crouch", crouch);
  57. //only control the player if grounded or airControl is turned on
  58. if (m_Grounded || m_AirControl)
  59. {
  60. // Reduce the speed if crouching by the crouchSpeed multiplier
  61. move = (crouch ? move*m_CrouchSpeed : move);
  62. // The Speed animator parameter is set to the absolute value of the horizontal input.
  63. m_Anim.SetFloat("Speed", Mathf.Abs(move));
  64. // Move the character
  65. m_Rigidbody2D.velocity = new Vector2(move*m_MaxSpeed, m_Rigidbody2D.velocity.y);
  66. // If the input is moving the player right and the player is facing left...
  67. if (move > 0 && !m_FacingRight)
  68. {
  69. // ... flip the player.
  70. Flip();
  71. }
  72. // Otherwise if the input is moving the player left and the player is facing right...
  73. else if (move < 0 && m_FacingRight)
  74. {
  75. // ... flip the player.
  76. Flip();
  77. }
  78. }
  79. // If the player should jump...
  80. if (m_Grounded && jump && m_Anim.GetBool("Ground"))
  81. {
  82. // Add a vertical force to the player.
  83. m_Grounded = false;
  84. m_Anim.SetBool("Ground", false);
  85. m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
  86. }
  87. }
  88. private void Flip()
  89. {
  90. // Switch the way the player is labelled as facing.
  91. m_FacingRight = !m_FacingRight;
  92. // Multiply the player's x local scale by -1.
  93. Vector3 theScale = transform.localScale;
  94. theScale.x *= -1;
  95. transform.localScale = theScale;
  96. }
  97. }
  98. }