У меня есть игрок или персонаж, который может бегать или ходить и прыгать, но когда игрок находится на вершине лестницы, прыжок не работает. < /p>
Я понимаю, почему, но не знаю, как заставить прыжок работать.
// Whether or not a player can steer while jumping;
[SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
[SerializeField] private Transform m_GroundCheck;
const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
private bool m_Grounded;
// Whether or not the player is grounded.
private void FixedUpdate()
{
bool wasGrounded = m_Grounded;
m_Grounded = false;
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
// This can be done using layers instead but Sample Assets will not overwrite your project settings.
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
Debug.Log(colliders[i].gameObject.tag);
m_Grounded = true;
if (!wasGrounded)
OnLandEvent.Invoke();
}
}
}
< /code>
Colliders [i] .gameObject находит наземные объекты, поэтому я понимаю, что делает код, но не включает в себя игровой объект для лестницы, который имеет название тега «Ladder_tall» в Colliders [i].public void Move(float moveH, float moveV, bool crouch, bool jump, bool isHorizonal)
{
.....
if (m_Grounded && jump)
{
Debug.Log(jump + " " + m_Grounded);
// Add a vertical force to the player.
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
}
< /code>
Таким образом, приведенный выше код позволяет игроку или символу прыгать, когда M_ -Granled является истинной, а прыжок передается из класса Playermocement установлен на false: < /p>
Это движение игрока: < /p>
private void FixedUpdate()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, verticalMove * Time.fixedDeltaTime, false, jump, isHorizontalMove);
//Debug.Log("FixedUpdate jump " + true);
jump = false;
//isLadder = false;
}
if (Input.GetButtonDown("Jump"))
{
//Debug.Log("Climb " + animator.GetBool("Climb"));
if (animator.GetBool("Climb") == true)
{
//Debug.Log("jump " + false);
jump = false;
}
else
{
//Debug.Log("jump " + true);
jump = true;
}
}
< /code>
Итак, посмотрите на код, как добавить лестничный коллайдер в: < /p>
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
Так что я могу сказать, что когда игрок находится на вершине лестницы, M_ -Gralhed равен true?
У меня есть игрок или персонаж, который может бегать или ходить и прыгать, но когда игрок находится на вершине лестницы, прыжок не работает. < /p> Я понимаю, почему, но не знаю, как заставить прыжок работать.[code]// Whether or not a player can steer while jumping; [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character [SerializeField] private Transform m_GroundCheck;
const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded private bool m_Grounded; // Whether or not the player is grounded. private void FixedUpdate() { bool wasGrounded = m_Grounded; m_Grounded = false;
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground // This can be done using layers instead but Sample Assets will not overwrite your project settings. Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround); for (int i = 0; i < colliders.Length; i++) { if (colliders[i].gameObject != gameObject) { Debug.Log(colliders[i].gameObject.tag); m_Grounded = true; if (!wasGrounded) OnLandEvent.Invoke(); } } } < /code> Colliders [i] .gameObject находит наземные объекты, поэтому я понимаю, что делает код, но не включает в себя игровой объект для лестницы, который имеет название тега «Ladder_tall» в Colliders [i].public void Move(float moveH, float moveV, bool crouch, bool jump, bool isHorizonal) {
.....
if (m_Grounded && jump) { Debug.Log(jump + " " + m_Grounded); // Add a vertical force to the player. m_Grounded = false; m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce)); } } < /code> Таким образом, приведенный выше код позволяет игроку или символу прыгать, когда M_ -Granled является истинной, а прыжок передается из класса Playermocement установлен на false: < /p> Это движение игрока: < /p> private void FixedUpdate() { controller.Move(horizontalMove * Time.fixedDeltaTime, verticalMove * Time.fixedDeltaTime, false, jump, isHorizontalMove); //Debug.Log("FixedUpdate jump " + true);
jump = false; //isLadder = false; }
if (Input.GetButtonDown("Jump")) { //Debug.Log("Climb " + animator.GetBool("Climb")); if (animator.GetBool("Climb") == true) { //Debug.Log("jump " + false); jump = false; } else { //Debug.Log("jump " + true); jump = true; } } < /code> Итак, посмотрите на код, как добавить лестничный коллайдер в: < /p> Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround); [/code] Так что я могу сказать, что когда игрок находится на вершине лестницы, M_ -Gralhed равен true?