First, not sure why it says "protected"lol. But anyway…
I got the Z key to let you high jump after pressing it, but how do I make it so he can go through terrain? For example, I have a layer called “ceiling”, so you will be able to high jump through that. But I also need it to ignore the ground check or else pressing Z would cause him to fall through the floor hes on.
protected Rigidbody2D rBody;
protected bool facingRight = true;
protected float moveDir;
public float spead;
public float jumpForce; //how high the character jumps
public float highJumpForce; //junps higher after pressing Z
private float moveInput; //the key being pressed down
private Rigidbody2D rb;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
void Start()
{
rb = GetComponent<Rigidbody2D>();
//Set a starting velocity when our Game Starts (Set x to 1 for moving Right and -1 for moving Left)
Vector3 DefaultVelocity = new Vector3(1 * spead, 0, 0);
rb.velocity = DefaultVelocity;
moveDir = spead;
}
//---------------------------------------------------------------------------------------------------------
void FixedUpdate()
{
//jump
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
//move left and right with left and right arrow keys
moveInput = moveInput = Input.GetAxisRaw("Horizontal"); //moveInput = Input.GetAxis("Horizontal") caused him to brifely stop when hitting the arrow again. So changed it to "GetAxisRaw" fixed it
//True if there is no Input
if (moveInput == 0f)
{
//We will try to use rb.velocity.x existing Velocity/Movement instead
//True if our Object is already moving
if (rb.velocity.x > 0f) //True if Moving Right
moveInput = 1f;
else if (rb.velocity.x < 0f) //True if Moving Left
moveInput = -1f;
//If both Statements failed, there is no Horizontal Movement
//moveInput will continue to be at 0
}
//True if there is Input OR we managed to use the existing Velocity instead to fill the value of moveInput
if (moveInput != 0f)
//Continue as per normal
rb.velocity = new Vector3(moveInput * spead, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{ //because he is moving left while facing right
Flip();
}
}
//------------------------------------------------------------------------
public void Update()
//press key to jump
{
float input = Input.GetAxis("Horizontal");
if (input > 0 && moveDir < 0)
{
moveDir = spead;
Flip();
}
else if (input < 0 && moveDir > 0)
{
moveDir = -spead;
Flip();
}
//CODING THE SPACE BAR TO JUMP
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce);
rb.velocity = Vector3.up * jumpForce;
SoundManager.PlaySound("jump");
}
rb.velocity = new Vector2(moveDir, rb.velocity.y);
//CODING THE Z KEY TO HIGH JUMP
if(isGrounded && Input.GetKeyDown(KeyCode.Z))
{
rb.AddForce(Vector3.up * highJumpForce);
rb.velocity = Vector3.up * highJumpForce;
}
}
void Flip() {
facingRight = ! facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}