transform.LookAt正在脱离游戏对象

时间:2017-06-18 15:35:53

标签: c# unity3d unity5

我正在施放一些咒语,我正在按LookAt设定方向。问题是LookAt将我的拼写动画设置为游戏对象。我从中获取位置的对象具有比例(3,3,3),网格渲染器,球体对撞机和刚体。 (其他碰撞器在子对象上)。这是我用于施法的代码:

public void castSpell(GameObject caster, Transform otherTransform, float duration)
{
    if(animationEnabled)
    {
        foreach(var a in animator)
        {
            foreach(var b in a.bools)
            {
                a.animator.SetBool(b.parameterName, b.parameterValue);
            }
            foreach(var i in a.ints)
            {
                a.animator.SetInteger(i.parameterName, i.parameterValue);
            }
            foreach(var f in a.floats)
            {
                a.animator.SetFloat(f.parameterName, f.parameterValue);
            }
        }
    }

    GameObject Temporary_Spell_Handler;
    Temporary_Spell_Handler = Instantiate(_Spell, Spell_Emitter.transform.position, Spell_Emitter.transform.rotation) as GameObject;

    ParticleSystemRenderer pr = Temporary_Spell_Handler.GetComponent<ParticleSystemRenderer>();
    float dist = Vector3.Distance(caster.transform.position, otherTransform.position);

    //Add Spell Script to the casted spell so it handes damage and everything about spells.
    Spell tempSpell = Temporary_Spell_Handler.GetComponent<Spell>();
    tempSpell.caster = caster;

    if(b_lenghtScale)
    {
        pr.lengthScale = -lenghtScale;
    }

    if(lookAtEnemy)
    {
        Temporary_Spell_Handler.transform.LookAt(otherTransform);
    }

    Destroy(Temporary_Spell_Handler, duration);
}

以下是图片的样子:

enter image description here

我发现了问题。我的ball缩放为(3,3,3),因此它上升并且物体的枢轴保持向下。那么我该如何克服这个问题呢?enter image description here

1 个答案:

答案 0 :(得分:1)

我创建了空gameobject并将其设为我ball的父级。然后我将gameobjects位置(现在是枢轴)设置到我不会(在球的中心y = 5)的位置,然后在球上我做了相反的(y = -5)。

然后在我创建的游戏对象我添加了标记pivotChange,然后在我的castSpell脚本中我在lookAtEnemy部分进行了此更改:

    if(lookAtEnemy)
    {
        if(other.transform.parent != null && other.transform.parent.gameObject.tag == "pivotChange")
        {
            Temporary_Spell_Handler.transform.LookAt(other.transform.parent.gameObject.transform);
        }
        else
        {
            Temporary_Spell_Handler.transform.LookAt(other.transform);
        }
    }

它工作正常。