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

How can I get Child objects' Rigidbody Component?

Discussion in 'Scripting' started by Yegeon2, Apr 17, 2020.

  1. Yegeon2

    Yegeon2

    Joined:
    Mar 15, 2020
    Posts:
    10
    How can I get Rigidbody Component of Child object?

    I want to get Rigidbody component of Waist of Character,
    and rotate it.

    Now, my script rotate not only Waist but also Entire body.

    upload_2020-4-17_22-4-50.png

    Thanks!
     
  2. SpartianTheDev

    SpartianTheDev

    Joined:
    Jan 17, 2019
    Posts:
    11
    Maybe a public variable or
    Code (CSharp):
    1. Rb = transform.GetChild(0).GetComponent<Rigidbody>();
     
  3. Yegeon2

    Yegeon2

    Joined:
    Mar 15, 2020
    Posts:
    10
    Thanks to your reply!
    I change the code below that you recommended.
    But, It does not work. Is there any mistake? (I'm noob)

    Rigidbody rb; (I am already using variable name rb to move Character)
    Rigidbody rb1; (for rotate waist only)

    void Start() {
    rb = GetComponent<Rigidbody>();
    rb1 = transform.GetChild(0).GetComponent<Rigidbody>();
    }

    void Update(){
    float _xRotation = Input.GetAxisRaw("Mouse Y");
    Vector3 _characterRotationX = new Vector3(-(_xRotation), 0f, 0f) * lookSensitivity;
    rb1.MoveRotation(rb1.rotation * Quaternion.Euler(_characterRotationX));
    }
     
  4. SpartianTheDev

    SpartianTheDev

    Joined:
    Jan 17, 2019
    Posts:
    11
    Sorry, my bad. Try this:
    Code (CSharp):
    1. Vector3 _characterRotationX = new Vector3(-(_xRotation), 0f, 0f) * lookSensitivity * Time.deltaTime;
     
  5. Yegeon2

    Yegeon2

    Joined:
    Mar 15, 2020
    Posts:
    10
    many Thanks Sir!! :)
    below 4 sentences are right? I'm not sure (because I'm noob)
    there is no error, but it doesn't work yet. (Is there any way to check simply that GetComponent rb1 propely?)

    Rigidbody rb1;
    rb1 = transform.GetChild(0).GetComponent<Rigidbody>();
    Vector3 _characterRotationX = new Vector3(-(_xRotation), 0f, 0f) * lookSensitivity * Time.deltaTime;
    rb1.MoveRotation(rb1.rotation * Quaternion.Euler(_characterRotationX));
     
  6. SpartianTheDev

    SpartianTheDev

    Joined:
    Jan 17, 2019
    Posts:
    11
    1. Try GetAxis() instead of GetAxisRaw()
    2. You can make rb1 a public variable and validate through editor.
    3. If that wont work, make _characterRotationX a public global variable and test it on runtime. Is it always 0?
     
  7. Yegeon2

    Yegeon2

    Joined:
    Mar 15, 2020
    Posts:
    10
    public variable _xRotation is work!
    I find a solution!
    if Is Kinematic -> true, waist can rotate. but, character cannot move!
     
    Last edited: Apr 18, 2020
  8. SpartianTheDev

    SpartianTheDev

    Joined:
    Jan 17, 2019
    Posts:
    11
    I have a question: Is there a specific reason for rotating with rb instead of transform?
     
  9. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
  10. Yegeon2

    Yegeon2

    Joined:
    Mar 15, 2020
    Posts:
    10
    there is no specific reason! I know that method only! (I'm noob)
    could you let me know another method to rotate the ragdoll waist?
     
  11. SpartianTheDev

    SpartianTheDev

    Joined:
    Jan 17, 2019
    Posts:
    11