PlatformerCharacter2D.cs 5.1 KB

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