Saturday, May 28, 2022
HomeGame Developmentunity - Rotate an object relative to the participant to face one...

unity – Rotate an object relative to the participant to face one other object


Basically, in my sport, I’ve 3 objects: a participant, a defend and a puck.
I need to have the defend orbit the participant to routinely face the puck. (So the place will change with the rotation).

Rework.LookAt() works for having the defend have a look at the puck, however I am unable to appear to determine the mathematical wizardry required to use that rotation to the relative place of the participant.

public class Protect : MonoBehaviour
{
    [SerializeField]
    personal GameObject playerObject;

    personal GameObject puck;
    personal Vector3 offset;
    
    void Begin()
    {
        GetPuckObject();
        offset = remodel.place - playerObject.remodel.place;
    }
    
    void LateUpdate()
    {
        remodel.place = playerObject.remodel.place + offset;

        if (!puck)
        {
            if (!GetPuckObject())
                return;
        }
        
        remodel.LookAt(puck.remodel);
    }

    bool GetPuckObject()
    {
        puck = GameObject.FindWithTag("Puck");
        return puck;
    }
} 

I really feel so dumb for asking for assist with this and I am positive it has been requested earlier than however I am unable to discover something which yields the behaviour I describe (presumably as a result of I am struggling to explain it). I believe it is a case of translating the rotation to a place however undecided the best way to strategy this.

Edit
This strikes in relation to the rotation however would not work correctly previous 180 levels (and is questionable earlier than that)

  void LateUpdate()
    {
        if (!puck)
        {
            remodel.place = playerObject.remodel.place + offset;
            if (!GetPuckObject())
                return;
        }
        remodel.place = playerObject.remodel.place + offset;
        Quaternion lookRotation = Quaternion.LookRotation(puck.remodel.place - remodel.place);
        remodel.rotation = lookRotation;
        remodel.Translate(remodel.ahead);
        Debug.Log(remodel.ahead);
    }

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments