- Home /
[RESOLVED] Pressing the "Z" key to make him go through certain terrain objects?
First of all, thanks for all the help the last person gave me, but unfortunately, this issue is quite stubborn.
LINES 120-140
Pressing the "Z" is supposed to make him go through certain terrain, only the ones with the layers that I chose. This script is not even reading the "IgnoreLayerCollision" parts, but it was reading the colliders parts, I commented those out to test the layers part, which is how I found out that part wasnt working.
Right now, he cant go through it, and only use one frame to go through it. All that happens is he hits his head instead of going through it
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Analytics;
public class WizardPlayerContoller : MonoBehaviour
{
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;
Physics.IgnoreLayerCollision(10, 11, false);
//gameObject.GetComponent<Collider2D>().enabled = false;
StartCoroutine(EnablePlayerCollider2D());
}
}
private IEnumerator EnablePlayerCollider2D() //re-enable player collider
{
yield return new WaitForSeconds(0.5f);
Physics.IgnoreLayerCollision(10, 11, true); //re-enables collider after landing back down
// gameObject.GetComponent<Collider2D>().enabled = true;
}
//-----------------------------------------------------------------------------
void Flip() {
facingRight = ! facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
Answer by Aviryx · Aug 19, 2020 at 02:41 AM
line 127: Physics.IgnoreLayerCollision(10, 11, false);
needs to be Physics2D.IgnoreLayerCollision(10, 11, true);
Line 138: Physics.IgnoreLayerCollision(10, 11, true);
needs to be Physics2D.IgnoreLayerCollision(10, 11, false);
https://docs.unity3d.com/ScriptReference/Physics2D.IgnoreLayerCollision.html
yep those arent typos and its the actual code.
changed it to what you had and still the script didnt read it.
yeah my mistake again. I forgot to change the boolean values (it's like 4am where I am). Updated answer. Pray to the Unity gods that works (or someone else answers) because I'm going to bed lol
it works thanks! I kept the collider stuff commented out, so I can get rid of those.