Search Unity

Bugging When I Hit The Wall and Jump "From It"

Discussion in 'Editor & General Support' started by FGPArthurVII, Jul 17, 2016.

  1. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    Almost ll the details are in the commentary note I've made in the very end of the script to try to fix later. Another important fact I need to say is that the collision of the scenario is made with Edge Collider 2D, so the collider of walls and to trap you on the map are pretty much the same. Could someone please help me?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.     public Rigidbody2D rBody;
    7.     public float jumpHeigth;
    8.     public float moveSpeed;
    9.     public Vector2 antigrav;
    10.     private Animator anim;
    11.     private bool isJumping;
    12.  
    13.     void Start ()
    14.     {
    15.         rBody = GetComponent<Rigidbody2D>();
    16.         anim = GetComponent<Animator>();
    17.     }
    18.  
    19.     void FixedUpdate ()
    20.     {
    21.         anim.SetBool ("isWalking", false);
    22.         if (Input.GetKey(KeyCode.RightArrow))
    23.         {
    24.             anim.SetBool("isWalking", true);
    25.             rBody.velocity = new Vector2(moveSpeed, rBody.velocity.y);
    26.         }
    27.         if (Input.GetKey(KeyCode.LeftArrow))
    28.         {
    29.             rBody.velocity = new Vector2(-moveSpeed, rBody.velocity.y);
    30.             anim.SetBool("isWalking", true);
    31.         }
    32.         if (Input.GetKeyDown(KeyCode.UpArrow) && !isJumping)
    33.         {
    34.             rBody.velocity = new Vector2 (rBody.velocity.x, jumpHeigth);
    35.             isJumping = true;
    36.             Debug.Log ("isJumping var is " + isJumping + " (Up)");
    37.         }
    38.     }
    39.     void OnCollisionEnter2D(Collision2D coll)
    40.     {
    41.         if (coll.gameObject.tag == "Floor")
    42.         {
    43.             isJumping = false;
    44.             Debug.Log ("isJumping var is " + isJumping + " (Down)");
    45.         }
    46.     }
    47.  
    48.     /* The void bellow disconsider the force of the gravity pulling down on ramp climbing to
    49. keep the game's speed on every situation; */
    50.     void OnTriggerStay2D(Collider2D trigg)
    51.     {
    52.         if (Input.GetKey (KeyCode.LeftArrow) && !isJumping && trigg.gameObject.tag == "AntigravityL")
    53.         {
    54.             rBody.AddForce (antigrav);
    55.         }
    56.         if (Input.GetKey (KeyCode.RightArrow) && !isJumping && trigg.gameObject.tag == "AntigravityR")
    57.         {
    58.             rBody.AddForce (antigrav);
    59.         }
    60.     }
    61. }
    62.  
    63. /* Notes:
    64.          Fix the wall bug related to jumping:
    65.              - When hitting the wall in mid-air and jumping again, you're unable to jump again.
    66.             Noticed: The cause for this doesn't seems to be related to the "isJumping" boolean.
    67.  
    68.             - sometimes then you press UpArrow he doesn't jump.
    69.             Noticed: The cause for this doesn't seems to be related to the "isJumping" boolean.
    70.  
    71.         How do I make the player's character's new scale his new standard scale without changing
    72.         the size it is now(A.k.A. new 0 value scale or "Freeze transformations" (This second one
    73.         is known by Autodek Maya Users))?
    74. */
    Thanks;
    Arthur.