Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Why does my character glitch when it lands

Discussion in 'Editor & General Support' started by Abubakr030109, Dec 24, 2020.

  1. Abubakr030109

    Abubakr030109

    Joined:
    Dec 24, 2020
    Posts:
    3
    For some reason, my player character glitches when it hits the ground(containing a BoxCollider2D and a "Ground" tag).

    By 'glitch' I mean:
    • When it spawns in the air it de-spawns when colliding with the ground, spawns in the air again then the cycle repeats
    • When spawns on the ground, the animations work, but it doesn't move at all.

    Player RigidBody2D details and Script values:
    Screenshot 2020-12-25 013717.jpg

    Player Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     Rigidbody2D rb;
    8.     float xMov;
    9.     bool facingRight = true;
    10.     Animator playerAnims;
    11.     bool isGrounded = false;
    12.     [SerializeField] float speed = 1f;
    13.     [SerializeField] GameObject BloodVFX;
    14.     [SerializeField] float jumpForce;
    15.  
    16.     void Awake()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.         playerAnims = GetComponent<Animator>();
    20.     }
    21.  
    22.     void FixedUpdate()
    23.     {
    24.         MovementLogic();
    25.     }
    26.  
    27.     void MovementLogic()
    28.     {
    29.         xMov = Input.GetAxisRaw("Horizontal");
    30.         rb.velocity = new Vector2(xMov * speed, rb.velocity.y);
    31.         if ((facingRight && xMov < 0) || (!facingRight && xMov > 0))
    32.         {
    33.             Flip();
    34.         }
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         if (isGrounded && xMov != 0)    //Running Phase
    40.         {
    41.             playerAnims.enabled = true;
    42.             playerAnims.SetBool("isRunning", true);
    43.         }
    44.         else if (!isGrounded)           //Jumping Phase          
    45.         {
    46.             playerAnims.enabled = false;
    47.         }
    48.         else                            //Idle Phase
    49.         {
    50.             playerAnims.enabled = true;
    51.             playerAnims.SetBool("isRunning", false);
    52.         }
    53.         if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W)) && isGrounded)
    54.         {
    55.             rb.velocity = Vector2.up * jumpForce;
    56.         }
    57.     }
    58.  
    59.     void Flip()
    60.     {
    61.         facingRight = !facingRight;
    62.         Vector3 scaler = transform.localScale;
    63.         scaler.x *= -1;
    64.         transform.localScale = scaler;
    65.     }
    66.  
    67.     void OnCollisionEnter2D(Collision2D collider)
    68.     {
    69.         if (collider.gameObject.tag == "Ground")
    70.         {
    71.             isGrounded = true;
    72.         }
    73.     }
    74.     void OnCollisionExit2D()
    75.     {
    76.         isGrounded = false;
    77.     }
    78.  
    79.     void OnTriggerEnter2D(Collider2D collider)
    80.     {
    81.         if (collider.tag == "Spikes")
    82.         {
    83.             playerAnims.SetBool("Died", true);
    84.             GameObject deathVFX = Instantiate(BloodVFX, transform.position, Quaternion.Euler(-90, 0, 0));
    85.             Destroy(deathVFX, 1);
    86.         }
    87.     }
    88.  
    89. }
    90.  

     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Is your ground or an object on your ground tagged as "Spikes"? Do you have any other scripts involved, such as on the ground?

    One error I'm noting in your code is that your OnCollisionExit2D does not have the proper method signature. It is supposed to have a Collision2D parameter (much like OnCollisionEnter2D). Without that parameter, Unity will not call your OnCollistionExit2D. This may explain some of your animation issues.
     
  3. Abubakr030109

    Abubakr030109

    Joined:
    Dec 24, 2020
    Posts:
    3
    There is a GameObject on the ground tagged as "Spikes". I've tried adding a parameter to the OnCollisionExit2D, but sadly nothing changed.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Doesn't your code destroy your player if it touches the spikes? Is it touching the spikes?
     
  5. Abubakr030109

    Abubakr030109

    Joined:
    Dec 24, 2020
    Posts:
    3
    No, I placed the player next to the spikes so it collides with the ground first.