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.

Quaternion.LookRotation, RotateTowards screw up GameObj's Mesh on Child Object

Discussion in 'Scripting' started by xuchun9000, Oct 9, 2019.

  1. xuchun9000

    xuchun9000

    Joined:
    Feb 24, 2017
    Posts:
    16
    hello, I have a MasterCube and a ChildCube attach to MasterCube, I want the ChildCube to turn and aim to the Target Object;

    when I try to use Quaternion.LookRotation and RotateTowards on ChildCube let it turn, the Mesh of the ChildCube totally screw up, I have no idea why?

    but If the MasterCube is an empty GameObject has no Mesh, the ChildCube runs perfectly. very strange

    What mistake did I made?

    thanks

    the code is very simple,the code is attached to MasterCube, and try to rotate child GameObject
    Code (CSharp):
    1. public class RotateScript : MonoBehaviour
    2. {
    3.     public GameObject target;
    4.     public GameObject child;  
    5.  
    6.     // Update is called once per frame
    7.     void Update()
    8.     {
    9.         Vector3 v3 = target.transform.position - this.child.transform.position;
    10.         Quaternion rotateTo = Quaternion.LookRotation(v3, Vector3.up);
    11.         this.child.transform.rotation = Quaternion.RotateTowards(this.child.transform.rotation, rotateTo, 30.0f * Time.deltaTime);
    12.     }
    13. }
    and the turning result is like this,
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,688
    The parent object is scaled unevenly and presumably also rotated. The scaling carries over to the children, so if you scaled the parent cube for its own appearance, the child cube will be warped the same way. With multiple levels of scaling/rotation, things get real weird.

    You can do a number of things to affect the shape of the parent cube while keeping its scale at 1,1,1. You can make the "parent cube"'s actual cube mesh a part of a child object, for example; keep the parent's scale at 1,1,1 and just scale the child. You could also install a plugin like ProBuilder and make the parent cube a mesh in the actual size you want it without needing to scale the transform.
     
  3. xuchun9000

    xuchun9000

    Joined:
    Feb 24, 2017
    Posts:
    16
    thanks StarManta: when I set the parent object's scale to be 1:1:1, everything works fine

    you rock...