0
\$\begingroup\$

I have a lightning effect that travels from point A to point B, I am having trouble placing placing point B correctly.

Object Hierarchy

As you can see from the object hierarchy the points (LightningStart, LightningEnd) are nested 3 levels deep. _parentReaction.affectorWidget.transform.positionis the position I would like the object to be at.

As you can see below I have tried setting the position equal to the other widgets transform, I have also tried using transform.InverseTransformDirection and transform.TransformDirection in varying combinations, I have tried many options the code below is just me going back to basics to see what I missed.

I am not sure even which object I am supposed to call TransformPoint from, the Widget with the effect )(Point A) or the Widget at Point B?

Given the above scenario how would I position LightningEnd so that is at the same position as_parentReaction.affectorWidget.transform.position?

I understand this is a very common question and I have read many previous questions related to this but two hours later, I would just like to know how its done so I can understand where I am getting this wrong.

Thank you.

Edit: _controller is the Widget Running this effect, since it spawns as a child of this I dont need to worry about its position.

    public override IEnumerator Do(Widget _controller, WidgetReaction _parentReaction)
{
    effect = _controller.GetComponent<Widget_Effect_Comp>().AddEffect(effect);

    while (true)
    {
        start = effect.effectPrefab.transform.Find("LightningStart").transform;
        end = effect.effectPrefab.transform.Find("LightningEnd").transform;

        Debug.Log("End Position : " + effect.effectPrefab.transform.Find("LightningEnd").transform.position);

        //Debug.Log("Widget World Pos : " + _controller.transform.position);
        //Debug.Log("Widget Local Pos : " + _controller.transform.localPosition);

        //Debug.Log("AffectorWidget World Pos : " + _parentReaction.affectorWidget.transform.position);
        //Debug.Log("AffectorWidget Local Pos : " + _parentReaction.affectorWidget.transform.localPosition);

        effect.effectPrefab.transform.Find("LightningEnd").transform.position = _parentReaction.affectorWidget.transform.position;

        Debug.Log("End Position : " + effect.effectPrefab.transform.Find("LightningEnd").transform.position);

     yield return new WaitForSeconds(2f);
    }

} 



public class WidgetEffect : ScriptableObject
{
    public string effectName;
    public GameObject effectPrefab;
}


public WidgetEffect AddEffect(WidgetEffect _widgetEffect)
{
    if (!activeEffects.ContainsKey(_widgetEffect))
    {
        GameObject go = Instantiate(_widgetEffect.effectPrefab);
        go.transform.parent = effectContainer.transform;
        go.transform.position = Vector3.zero;
        go.transform.localPosition = Vector3.zero;
        activeEffects.Add(_widgetEffect, go);

        return _widgetEffect;
    }
    return null;
}

Edit: Removed Extended Explanation not related to initial question! [![enter image description here]]

\$\endgroup\$
8
  • \$\begingroup\$ I may have spoke too soon, the first effect chains properly but the second (which in this case should go back to the first) is quite a distance off, that may be a separate question though :) \$\endgroup\$ – Pheonix2105 May 5 '20 at 21:51
  • \$\begingroup\$ Sounds like you may want to edit your question to be sure it contains a Minimal Complete Verifiable Example of that problem. \$\endgroup\$ – DMGregory May 5 '20 at 21:53
  • \$\begingroup\$ I think my problem is to do with how I have implemented my Do Ienumerator which starts each action and waits for them to finish, in order to keep the position updated I have to hold up that Ienumerator 'queue', Im sure there is a pattern name for the system I am working on but I do not know it! \$\endgroup\$ – Pheonix2105 May 5 '20 at 22:17
  • \$\begingroup\$ I'd need more context on your project to advise on that matter. Can you edit your question to explain what you're doing in more detail? \$\endgroup\$ – DMGregory May 5 '20 at 22:25
  • \$\begingroup\$ Sorry I was trying to word the question correctly and include the correct information without just dumping it all in the question, please let me know if you need any clarification, I have only included two functions from the Reaction Component but the rest are things like add/remove from lists. \$\endgroup\$ – Pheonix2105 May 5 '20 at 22:43
1
\$\begingroup\$

As @DmGregory pointed out, my problem was I was setting the position of the Prefab inspector reference instead of the Instance I had created In AddEffect and by also changing AddEffects Return Type to GameObject, returning go instead of _widgetEffect

Updated Code Below

public GameObject AddEffect(WidgetEffect _widgetEffect)
{
    if (!activeEffects.ContainsKey(_widgetEffect))
    {
        GameObject go = Instantiate(_widgetEffect.effectPrefab);
        go.transform.parent = effectContainer.transform;
        go.transform.position = Vector3.zero;
        go.transform.localPosition = Vector3.zero;
        activeEffects.Add(_widgetEffect, go);

        return go;
    }
    return null;
}

public override IEnumerator Do(Widget _controller, WidgetReaction _parentReaction)
{
    prefabInstance = _controller.GetComponent<Widget_Effect_Comp>().AddEffect(effect);
    start = prefabInstance.transform.Find("LightningStart").transform;
    end = prefabInstance.transform.Find("LightningEnd").transform;

    Debug.Log("End Position : " + end.position);

    //Debug.Log("Widget World Pos : " + _controller.transform.position);
    //Debug.Log("Widget Local Pos : " + _controller.transform.localPosition);

    //Debug.Log("AffectorWidget World Pos : " + _parentReaction.affectorWidget.transform.position);
    //Debug.Log("AffectorWidget Local Pos : " + _parentReaction.affectorWidget.transform.localPosition);
    start.position = _controller.transform.position;
    end.position = _parentReaction.affectorWidget.transform.position;

    Debug.Log("End Position : " + end.position);

    yield break;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.