So i have problem with jumping. When ClimbLadder() function is not in Update so it isnt turn on player is normally jumping. But when ClimbLadder() function is in update player can not jump. Only when i press some vertical key and symontenously pressing space. Sometimes will player jump. I not really know whats happening. The layers are set correctly and same with collisions. Can someone tell me whats the problem ?
Video: https://www.youtube.com/watch?v=iZNQtamKvKE
Player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Player : MonoBehaviour
{
// Config
[SerializeField] float runSpeed = 5f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float climbSpeed = 5f;
// State
bool isAlive = true;
// Cashed component references
Rigidbody2D myRigidBody;
Animator myAnimator;
Collider2D myCollider2D;
//Message then methods
// Start is called before the first frame update
void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
myCollider2D = GetComponent<Collider2D>();
}
// Update is called once per frame
void Update()
{
Run();
FlipSprite();
Jump();
ClimbLadder();
}
private void Run()
{
float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal"); // -1 to +1
Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
myRigidBody.velocity = playerVelocity;
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
}
private void Jump()
{
if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }
if (CrossPlatformInputManager.GetButtonDown("Jump"))
{
Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
myRigidBody.velocity += jumpVelocityToAdd;
}
}
private void ClimbLadder()
{
if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Climbing"))) { return; }
float controlThrow = CrossPlatformInputManager.GetAxis("Vertical");
Vector2 climbVelocity = new Vector2(myRigidBody.velocity.x, controlThrow * climbSpeed);
myRigidBody.velocity = climbVelocity;
}
private void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1f);
}
}
}