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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to stop a RigidBody that is moving

Discussion in 'Physics' started by RonErez, Sep 27, 2016.

  1. RonErez

    RonErez

    Joined:
    Aug 30, 2016
    Posts:
    13
    i gave a rigidbody a velocity at void start(), and i want it to stop once it reaches a certain point in the screen.
    tryed doing rb.velocity = Vector3.zero and such, but couldnt find any thing thet worked even after searching the forum.
    Code (CSharp):
    1. void Start()
    2.     {
    3.         changedSpeed = false;
    4.         rb = GetComponent<Rigidbody> ();
    5.         if (this.tag == "Boss") {
    6.             rb.velocity = transform.forward * speed;
    7.             ///////
    8.         } else {
    9.             rb.velocity = transform.forward * speed;
    10.         }
    11.     }
    12.  
    13.     void FixedUpdate ()
    14.     {
    15.         if (this.tag == "Boss") {
    16.             rb = GetComponent<Rigidbody> ();
    17.             if ((rb.position.z <= 13.5) && (changedSpeed == false)) {
    18.                 string messege = string.Format("position z = {0}, speed = {1}", rb.position.z, rb.velocity.ToString());
    19.                 Debug.Log (messege);
    20.                 rb.velocity = Vector3.zero;
    21.                 rb.angularVelocity = Vector3.zero;
    22.                 rb.angularDrag = 0;
    23.                 messege = string.Format("position z = {0}, speed = {1}", rb.position.z, rb.velocity.ToString());
    24.                 Debug.Log (messege);
    25.                 changedSpeed = true;
    26.             }
    27.         }
    28.     }
     
  2. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Well your object should stop when you set velocity to zero. Are you adding force anywhere else? Do you have any friction? Are your Debug.Log called?

    As a last resort you could set rb.isKinematic to true. When you want you object to move again, you will need to set it back to false
     
  3. RonErez

    RonErez

    Joined:
    Aug 30, 2016
    Posts:
    13
    ohh S***, i saved the starting speed in another script and inputed velocity there.... thanks!