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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to set the velocity of a GameObject to Zero

Discussion in '2D' started by unity_13glassj, Jul 20, 2020.

  1. unity_13glassj

    unity_13glassj

    Joined:
    Jun 8, 2020
    Posts:
    4
    After my player dies, I use transform.position to move it back to the respawn point.
    Code (CSharp):
    1. transform.position = RespawnPoint;
    The problem is, it still retains its velocity from before it died.
    This often causes it to fall off the platform again! Please Help!
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    unity_13glassj likes this.
  3. cyclSunity

    cyclSunity

    Joined:
    Jun 10, 2020
    Posts:
    123
    I just need to ask this for my game, but, does an object with a Kinematic RigidBody2D type and a box collider collide with another box?
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Yes. Non-trigger colliders will always collide with other colliders unless explicitly disabled in the physics collision matrix.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Only specific Rigidbody2D types (and their colliders) will interact. If you want to know what body-types can collide with other body-types then you can find that info here (see "Collision action matrix" section).

    FYI: You should not be modifying the Transform when using Rigidbody2D or any other 2D physics component. By adding a Rigidbody2D you're putting it in control of the Transform so you're bypassing it by doing that. The Rigidbody2D has its own API for doing this i.e. its position, velocity etc.
     
    unity_13glassj likes this.
  6. unity_13glassj

    unity_13glassj

    Joined:
    Jun 8, 2020
    Posts:
    4
    Thanks Vryken and MelvMay
    I am now using:
    Code (CSharp):
    1. rb.position = respawnPoint;
    2. rb.velocity = new Vector2(0,0);
     
    Last edited: Jul 21, 2020
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Cool.

    Note that there's a shortcut for zero on a Vector2 here,
     
  8. TobiasjBurford

    TobiasjBurford

    Joined:
    Feb 17, 2022
    Posts:
    11
    Code (CSharp):
    1.              GetComponent<Rigidbody2D>().velocity = Vector3.zero;
    2.  
    Sets the rigidbody velocity to zero
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    No, you should use Vector2.zero (rather than relying on Unity implicitly converting your incorrect type to the correct one) as stated in my post before your thread necro. ;)