2014-10-26 167 views
1

我有一个游戏对象,它沿着它的一个轴来回摆动,这个对象附着在围绕场景移动的父GameObject上。但是,当父对象旋转时,子对象不会随之旋转。使旋转对象与父对象一起旋转

我希望这个事情做与当地VS全球空间。但是林茫然......

这是管理的扫码。

// Increment a timer 
    timer += Time.deltaTime; 

    // Swing the object back and forth based on the rotationLimit and the rotationSpeed 
    currentRotation = Mathf.PingPong (timer * rotationSpeed, rotationLimit * 2f) - rotationLimit; 

    // Create a temporary variable to store the new rotation 
    Quaternion rotation = Quaternion.Euler (transform.rotation.x, transform.rotation.y + currentRotation, transform.rotation.z); 

    // Set the rotation to the temp var 
    transform.rotation = rotation; 

我考虑抓住父母的旋转和使用,而不是transform.rotation。然而,理想情况下,同一个脚本需要处理各种对象,其中一些对象没有父母。任何人都可以告诉我哪里出错了。

回答

1

从你的标签我猜你使用Unity。据我所知,团结支持'SceneGraph'的概念。这可以让你建立一个游戏对象树,在那里孩子们遵循他们父母的转变。 请参阅此链接:http://docs.unity3d.com/ScriptReference/Transform-parent.html

另类 - 如果你想手动做到这一点:

你应该小心,当手动合并父/子转换。要做到这一点,最简单的,也许最好的方法就是简单地将家长和孩子的转换:

ChildWorldTransform = ChildLocalTransform * ParentWorldTransform 

ChildLocalTransform对应于您currentRotation,ParentWorldTransform你的父对象的转变。请注意,您应该将整个转换作为一个整体相乘,而不仅仅是旋转部分!

我还建议你做一个谷歌'场景图'