Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Rigidbody MoveRotation for childs

Discussion in 'Physics' started by RootON, Jan 24, 2015.

  1. RootON

    RootON

    Joined:
    Nov 15, 2014
    Posts:
    1
    Hi.

    In Unity FixedUpdate() function I call next code:

    Code (CSharp):
    1.  
    2. public void Move(float x, float z)
    3. {
    4.        Vector3 targetPosition = new Vector3(x, transform.position.y, z);
    5.  
    6.         foreach (Transform child in transform)
    7.         {
    8.             if(child.rigidbody != null )
    9.             {
    10.                 Quaternion targetRotation = Quaternion.LookRotation(targetPosition - child.transform.position);
    11.                 child.rigidbody.MoveRotation( Quaternion.Slerp(child.transform.rotation, targetRotation, Time.deltaTime * 10f)  ); // NOT WORK
    12.             }
    13.         }
    14.  
    15. rigidbody.MovePosition( Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * moveSpeed) );
    16. }
    17.  
    When I change child.rigidbody.MoveRotation to child.transform.rotation:

    Code (CSharp):
    1. child.transform.rotation =Quaternion.Slerp(child.transform.rotation, targetRotation,Time.deltaTime *10f);
    everything works as I espect, but as I know its very bad practice to use "transform.rotation =" for rigidbody.

    Parent and all its child has "IsKinematic = true";

    Can you help me ? What`s wrong with child.rigidbody.MoveRotation ?
     
    Last edited: Jan 24, 2015
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Do the child objects really need to have rigidbodies at all? Maybe you could describe what type of object you are simulating here - from the code it looks like you might be pointing wheels or guns or something but it isn't clear exactly what effect you want to create.