简体   繁体   English

transform.LookAt正在关闭游戏对象

[英]transform.LookAt is going off the gameobject

I am casting some spell and i am setting it direction by LookAt . 我正在施放一些咒语,并通过LookAt设置方向。 Problem is that LookAt is setting my spell animation off the gameobject. 问题是LookAt正在将我的咒语动画设置在游戏对象之外。 Object from which i am getting position has scale (3, 3, 3), mesh renderer, sphere collider, and rigidbody. 我从中获得位置的对象具有比例(3、3、3),网格渲染器,球体碰撞器和刚体。 (other colliders are on child objects). (其他对撞机位于子对象上)。 Here is the code i am using for casting spell: 这是我用来投射咒语的代码:

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);
}

and here is the image how it looks like: 这是图片的样子:

在此处输入图片说明

I found the problem. 我发现了问题。 My ball is scaled to (3, 3, 3), so it went up and pivot of the object stayed down. 我的ball缩放到(3,3,3),所以它上升了,物体的枢轴保持了下来。 So how can I overcome this problem? 那么我该如何克服这个问题呢? 在此处输入图片说明

I created empty gameobject and make it a parent to my ball . 我创建了一个空的gameobject并使它成为我的ball的父对象。 Then i set gameobjects position (it is pivot now) to where i wont (around center of ball y=5) and then on the ball i did the opposite (y= -5). 然后我将gameobjects的位置(现在是枢轴)设置到我将不希望的位置(围绕球的中心y = 5),然后在球上进行相反的操作(y = -5)。

Then to the gameobject i created as pivot i added tag pivotChange and then in my castSpell script i made this change at lookAtEnemy part: 然后向我作为支点创建的游戏对象添加标签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);
        }
    }

And it is working okay. 而且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM