Search Unity

Cannot use "velocity" on a static body

Discussion in 'Scripting' started by Digital_Owl, Jan 14, 2022.

  1. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    whenever cannonball hits my character while jumping it just freezes, doesn't load losecene or play death animation, however when its in the idle or running position it does all of them normally, it gives me an error which says Cannot use "velocity" on a static body, i need to freeze the character, play death anim and then load the lose scene but i cannot trigger all 3 of these action while jumping although it perfectly works on any other state



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerLife : MonoBehaviour
    7.  
    8.  
    9. {
    10.     private Rigidbody2D rb;
    11.     private Animator anim;
    12.  
    13.  
    14.     void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody2D>();
    17.         anim = GetComponent<Animator>();
    18.      
    19.     }
    20.  
    21.  
    22.     private void OnCollisionEnter2D(Collision2D collision)
    23.     {
    24.         if (collision.gameObject.CompareTag("Enemy"))
    25.         {
    26.             Die();
    27.          
    28.         }
    29.     }
    30.  
    31.  
    32.     private void Die()
    33.     {
    34.         rb.bodyType = RigidbodyType2D.Static;
    35.         anim.SetTrigger("death");
    36.        
    37.  
    38.     }
    39.  
    40.     private void RestartLevel()
    41.     {
    42.         SceneManager.LoadScene("LoseScene");
    43.     }
    44. }
    45.  
    Capture.PNG
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Wherever you are moving the character, you need to check if it has been flagged static and stop doing so.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
  4. sorceryduelist

    sorceryduelist

    Joined:
    Jul 25, 2022
    Posts:
    1
    This helped me a lot as I was having a similar issue as I was using similar code for my Die() method.

    Code (CSharp):
    1. if (rb.bodyType != RigidbodyType2D.Static) {
    2.  
    3. }
    I added this into my movement script and it stopped spamming the static error in console. Thanks!
     
    LiteBits, arysapriatna12 and wlstj428 like this.