Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Making Z being able to go through terrain

Discussion in 'Scripting' started by SuperCrow2, Aug 18, 2020.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    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.

    Code (CSharp):
    1.  protected Rigidbody2D rBody;
    2.  
    3.     protected bool facingRight = true;
    4.     protected float moveDir;
    5.  
    6.     public float spead;
    7.     public float jumpForce; //how high the character jumps
    8.     public float highJumpForce; //junps higher after pressing Z
    9.  
    10.     private float moveInput; //the key being pressed down
    11.     private Rigidbody2D rb;
    12.  
    13.      private bool isGrounded;
    14.     public Transform groundCheck;
    15.     public float checkRadius;
    16.     public LayerMask whatIsGround;
    17.  
    18.  
    19.    void Start()
    20.     {
    21.         rb = GetComponent<Rigidbody2D>();
    22.         //Set a starting velocity when our Game Starts (Set x to 1 for moving Right and -1 for moving Left)
    23.         Vector3 DefaultVelocity = new Vector3(1 * spead, 0, 0);
    24.         rb.velocity = DefaultVelocity;
    25.         moveDir = spead;
    26.  
    27. }
    28.  
    29. //---------------------------------------------------------------------------------------------------------
    30.  
    31. void FixedUpdate()
    32.     {
    33.  
    34.         //jump
    35.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
    36.  
    37.  
    38.  
    39.  
    40.         //move left and right with left and right arrow keys
    41.  
    42.         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
    43.  
    44.  
    45.  
    46.         //True if there is no Input
    47.         if (moveInput == 0f)
    48.         {
    49.  
    50.             //We will try to use rb.velocity.x existing Velocity/Movement instead
    51.  
    52.             //True if our Object is already moving
    53.             if (rb.velocity.x > 0f)    //True if Moving Right
    54.                 moveInput = 1f;
    55.             else if (rb.velocity.x < 0f)   //True if Moving Left
    56.                 moveInput = -1f;
    57.             //If both Statements failed, there is no Horizontal Movement
    58.             //moveInput will continue to be at 0
    59.         }
    60.  
    61.  
    62.         //True if there is Input OR we managed to use the existing Velocity instead to fill the value of moveInput
    63.         if (moveInput != 0f)
    64.             //Continue as per normal
    65.             rb.velocity = new Vector3(moveInput * spead, rb.velocity.y);
    66.  
    67.  
    68.         if (facingRight == false && moveInput > 0)
    69.         {
    70.             Flip();
    71.  
    72.         }
    73.  
    74.  
    75.         else if (facingRight == true && moveInput < 0)
    76.         { //because he is moving left while facing right
    77.             Flip();
    78.         }
    79.     }
    80.     //------------------------------------------------------------------------
    81.     public void Update()
    82.     //press key to jump
    83.  
    84.     {
    85.  
    86.  
    87.         float input = Input.GetAxis("Horizontal");
    88.  
    89.         if (input > 0 && moveDir < 0)
    90.         {
    91.             moveDir = spead;
    92.             Flip();
    93.         }
    94.         else if (input < 0 && moveDir > 0)
    95.         {
    96.             moveDir = -spead;
    97.             Flip();
    98.         }
    99.  
    100.  
    101.         //CODING THE SPACE BAR TO JUMP
    102.  
    103.         if (isGrounded && Input.GetKeyDown(KeyCode.Space))
    104.         {
    105.             rb.AddForce(Vector3.up * jumpForce);
    106.             rb.velocity = Vector3.up * jumpForce;
    107.             SoundManager.PlaySound("jump");
    108.         }
    109.  
    110.         rb.velocity = new Vector2(moveDir, rb.velocity.y);
    111.  
    112.  
    113.  
    114.      //CODING THE Z KEY TO HIGH JUMP
    115.         if(isGrounded && Input.GetKeyDown(KeyCode.Z))
    116.     {
    117.             rb.AddForce(Vector3.up * highJumpForce);
    118.             rb.velocity = Vector3.up * highJumpForce;
    119.         }
    120.    }
    121.  
    122.  
    123.  
    124. void Flip() {
    125.  
    126.             facingRight = ! facingRight;
    127.             Vector3 Scaler = transform.localScale;
    128.             Scaler.x *= -1;
    129.             transform.localScale = Scaler;
    130.         }
    131.  
    132.  
    133.     }
    134.  
    135.  
     
  2. chaikadev

    chaikadev

    Joined:
    Nov 9, 2019
    Posts:
    6
    Maybe disable the character's collision box until it goes through the terrain?
     
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    I tried this, but it didnt work

    Code (CSharp):
    1.     public Transform BackgroundPrefab;
    2.  
    3.  
    4.       if(isGrounded && Input.GetKeyDown(KeyCode.Z))
    5.     {
    6.             rb.AddForce(Vector3.up * highJumpForce);
    7.             rb.velocity = Vector3.up * highJumpForce;
    8.             Physics.IgnoreCollision(BackgroundPrefab.GetComponent<Collider>(), GetComponent<Collider>());
    9.  
    10.         }
    11.    }
    12.