Search Unity

Unity2D, how can I make a solid game object on Unity 5?

Discussion in 'Scripting' started by ShiveringEmpress39, Jun 13, 2018.

  1. ShiveringEmpress39

    ShiveringEmpress39

    Joined:
    Apr 4, 2018
    Posts:
    7
    Hi guys, today while I was creating the player's movements I noticed that the ground was not solid, I tried to put a collider on the player and also a rigidbody2D and for the ground only a collider, but it does not work the same. I also tried using LayerMask but nothing, please help me this thing is leading me to madness xD.

    Here is the code :

    Code (CSharp):
    1.  public class PlayerController : MonoBehaviour {
    2.          public bool variablesOnAwake = true;
    3.          private Rigidbody2D rb;
    4.          public float moveSpeed;
    5.          public float jumpHeight;
    6.          public Transform groundCheck;
    7.          public float groundCheckRadius;
    8.          public LayerMask whatIsGround;
    9.          private bool grounded;
    10.          void Start() {
    11.              if(!variablesOnAwake) {
    12.                  rb = GetComponent<Rigidbody2D>();
    13.              }
    14.          }
    15.          void Awake() {
    16.              if(variablesOnAwake) {
    17.                  rb = GetComponent<Rigidbody2D>();
    18.              }
    19.          }
    20.          void FixedUpdate() {
    21.              grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
    22.              //////////////JUMP
    23.              if(Input.GetButtonDown("Jump")) {
    24.                
    25.                  Debug.Log("Is Jumping");
    26.                  rb.velocity = new Vector2(rb.velocity.y, jumpHeight);
    27.                
    28.              } else if (Input.GetButtonUp("Jump")) {
    29.                
    30.                  Debug.Log("Isn't jumping");
    31.                
    32.              }
    33.            
    34.              ////////////MOVEMENTS
    35.              if(Input.GetKeyDown(KeyCode.D)) {
    36.                  rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
    37.                
    38.              } else if (Input.GetKeyDown(KeyCode.A)) {
    39.                
    40.                  rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
    41.                
    42.              }
    43.          }
    44.      }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not solid, as in the player is falling through the ground?

    You have a sprite for the player & one for the ground? The ground has a collider (not marked as 'trigger') and the player has a collider + a rigidbody2D which is dynamic?
     
  3. ShiveringEmpress39

    ShiveringEmpress39

    Joined:
    Apr 4, 2018
    Posts:
    7
    Oh now i see, all my ground colliders were setted to trigged! Thx a lot, another question... Can you provide to me a good player controller script for a 2D platformer game?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
  5. ShiveringEmpress39

    ShiveringEmpress39

    Joined:
    Apr 4, 2018
    Posts:
    7