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

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.