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

Question I couldnt get Rigidbody to freeze X

Discussion in '2D' started by Ali_Fuat_ADAK1, Jul 26, 2023.

  1. Ali_Fuat_ADAK1

    Ali_Fuat_ADAK1

    Joined:
    Dec 1, 2021
    Posts:
    24
    I am trying to freeze the x value after the player dies because he just starts to slide even if the movement script is disabled i searched the documents but i couldnt really understand it
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerHealth : MonoBehaviour
    4. {
    5.     [SerializeField]private float startingHealth;
    6.     public float currentHealth { get; private set; }
    7.     private Animator anim;
    8.     private bool dead;
    9.     public RigidbodyConstraints2D const;
    10.  
    11.     private void Awake()
    12.     {
    13.         currentHealth = startingHealth;
    14.         anim = GetComponent<Animator>();
    15.         const = GetComponent<RigidbodyConstraints2D>();
    16.     }
    17.    
    18.    
    19.  
    20.     public void TakeDamage(float _damage)
    21.     {
    22.         currentHealth = Mathf.Clamp(currentHealth - _damage, 0, startingHealth);
    23.  
    24.         if(currentHealth > 0)
    25.         {
    26.             anim.SetTrigger("hurt");
    27.         }else{
    28.             if(!dead){
    29.                 anim.SetTrigger("die");
    30.                 RigidbodyConstraints2D.FreezeAll;
    31.                 GetComponent<PlayerMovement>().enabled = false;
    32.                 dead = true;
    33.                
    34.             }
    35.            
    36.         }
    37.     }
    38. }
    39.  
     
  2. Ali_Fuat_ADAK1

    Ali_Fuat_ADAK1

    Joined:
    Dec 1, 2021
    Posts:
    24
    If you have a better method of stopping the player after dying please share it
     
  3. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,342
  4. Kuan_Coder

    Kuan_Coder

    Joined:
    Apr 29, 2023
    Posts:
    4
    I'm not entirely sure if this would work, but could you just do:

    public Rigidbody2D rbname;

    //When dead (not !dead, which means you're still alive):

    rbname.enabled = false;
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,558
    This is a great idea but unfortunately Rigidbody does not inherit from that lineage and has no .enabled field.

    Some other options:

    - Destroy() the Rigidbody instance

    - set it to .isKinematic = true

    - use a FixedJoint and pin it in place
     
    Ali_Fuat_ADAK1 likes this.
  7. Kuan_Coder

    Kuan_Coder

    Joined:
    Apr 29, 2023
    Posts:
    4

    Sorry, my mistake. I've been using the .enabled field in my project with a Collider2D, not a rigidbody.